> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript Throw Statement

The `throw` statement is a control flow construct that raises an exception, immediately halting the execution of the current block. Control is transferred to the nearest enclosing `catch` clause in the call stack, executing any intervening `finally` blocks along the way. If no exception handler is found, the execution context is destroyed and the program terminates.

## Syntax

```typescript theme={"dark"}
throw expression;
```

## Technical Characteristics

**Expression Types**
TypeScript does not restrict the type of the `expression` being thrown. While it is standard practice to throw instances of the built-in `Error` class (or its subclasses) to preserve stack traces, the compiler permits throwing primitives, arbitrary objects, or even `null`.

```typescript theme={"dark"}
function throwStandardError() {
  throw new Error("Standard exception");
}

function throwString() {
  throw "String exception"; // Valid, but lacks stack trace
}

function throwObject() {
  throw { code: 500, message: "Object exception" };
}
```

**Control Flow and Reachability**
TypeScript's control flow analyzer treats `throw` as a terminal node. Any statements immediately following a `throw` within the same lexical scope are evaluated as unreachable code. By default (where the `allowUnreachableCode` compiler option is `undefined`), TypeScript provides editor suggestions such as fading the unreachable text, but it does not emit a compiler error or block compilation. The compiler will only emit an error (`TS7027: Unreachable code detected`) if `allowUnreachableCode` is explicitly set to `false` in the `tsconfig.json`.

```typescript theme={"dark"}
// Assumes tsconfig.json configures "allowUnreachableCode": false
function process(): void {
  throw new Error("Halt");
  console.log("Unreachable"); // TS Error: Unreachable code detected.
}
```

**The `never` Type**
When a function's execution path guarantees a `throw` statement (meaning it never successfully returns control to its caller), its return type is conceptually `never`. For function expressions and arrow functions, TypeScript automatically infers this `never` return type. However, for standard function declarations, TypeScript infers `void` for backward compatibility. Therefore, standard function declarations require an explicit `: never` annotation to enforce this bottom type.

```typescript theme={"dark"}
// TypeScript infers the return type as `never`
const failArrow = (message: string) => {
  throw new Error(message);
};

// Explicit `: never` annotation is required; otherwise inferred as `void`
function failDeclaration(message: string): never {
  throw new Error(message);
}
```

**Catch Block Typing**
Because a `throw` statement can emit any type at runtime, TypeScript cannot statically infer the type of the caught exception. If the `strict` family of compiler options is enabled (which automatically enables `useUnknownInCatchVariables`), the caught variable in a `try...catch` block is typed as `unknown`. If `strict` mode is disabled, the default type remains `any`. When the caught variable is `unknown`, developers must perform type narrowing on the value before safely accessing its properties.

```typescript theme={"dark"}
try {
  throw new Error("Failure");
} catch (error) {
  // In strict mode, 'error' is typed as 'unknown'. Type narrowing is required.
  if (error instanceof Error) {
    console.log(error.message); // Safely narrowed to Error
  } else {
    console.log(String(error)); // Handling non-Error throws
  }
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor TypeScript Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
