Skip to main content

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.

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

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.
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.
// 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 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.
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
  }
}
Master TypeScript with Deep Grasping Methodology!Learn More