---
url: 'https://adk.nht.io/api/@nhtio/adk/factories/functions/createException.md'
---

# Function: createException()

```ts
function createException<T>(
  name: string,
  message: string,
  code: string,
  status?: number,
  fatal?: boolean,
): CreatedException<T>;
```

Defined in: [lib/utils/exceptions.ts:59](https://github.com/NHTIO/ADK/blob/v1.20260605.0/src/lib/utils/exceptions.ts#L59)

Factory that produces a named [@nhtio/adk!BaseException](../classes/BaseException.md) subclass with a fixed printf-style message
template, error code, HTTP status, and fatality flag.

## Type Parameters

| Type Parameter        | Default type | Description                                                                                                                        |
| --------------------- | ------------ | ---------------------------------------------------------------------------------------------------------------------------------- |
| `T` *extends* `any`\[] | \[]         | Tuple of printf format argument types. Pass a non-empty tuple to require callers to supply interpolation values at the throw site. |

## Parameters

| Parameter | Type      | Description                                                                    |
| --------- | --------- | ------------------------------------------------------------------------------ |
| `name`    | `string`  | The `name` property set on thrown instances (used by isNamedException).        |
| `message` | `string`  | Printf-style template string for the error message.                            |
| `code`    | `string`  | Machine-readable error code stored on the static and instance `code` property. |
| `status?` | `number`  | HTTP status code associated with this exception class.                         |
| `fatal?`  | `boolean` | When `true`, signals that the error is unrecoverable.                          |

## Returns

[`CreatedException`](../type-aliases/CreatedException.md)<`T`>

A constructor for a [@nhtio/adk!BaseException](../classes/BaseException.md) subclass with the given metadata baked in.

## Remarks

Prefer this over hand-writing subclasses for simple, static exception definitions.

## Example

```ts
export const E_NOT_FOUND = createException<[string]>(
  "E_NOT_FOUND",
  "Resource %s not found",
  "E_NOT_FOUND",
  404,
  false,
);
throw new E_NOT_FOUND(["my-id"]);
```
