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 initiates a user-defined exception, immediately halting the normal execution flow of the current function or block. Control is transferred to the nearest enclosing catch block in the call stack. If the throw statement is executed within an async function or a Promise executor, it rejects the underlying Promise. In synchronous contexts where no enclosing catch block exists, the exception propagates to the global scope as an unhandled exception, which logs an error but does not necessarily terminate the entire script in modern JavaScript environments.

Syntax

throw expression;
The expression can be of any data type. The JavaScript engine evaluates the expression to determine the exception value that will be propagated.

Execution Mechanics

  1. Evaluation: The engine evaluates the expression immediately following the throw keyword.
  2. Control Flow Interruption: Normal execution is suspended. Subsequent statements within the current execution context are bypassed, with the strict exception of statements within an associated finally block. If a finally block exists, its statements are guaranteed to execute before the exception propagates further.
  3. Stack Unwinding: The engine traverses up the call stack searching for an active try...catch statement. During this unwinding process, any pending finally blocks encountered along the call stack are evaluated and executed in order.
  4. Exception Binding: Once a catch block is encountered, the evaluated exception value is intercepted. If the catch clause includes an identifier (e.g., catch (err)), the value is bound to that local variable. As of ES2019, catch binding is optional (e.g., catch { ... }); if the identifier is omitted, the exception is caught and control flow resumes, but the thrown value is not bound to any variable.

Syntax Visualization

While JavaScript permits throwing any primitive or object, throwing instances of the built-in Error object (or its subclasses like TypeError or ReferenceError) is the standard convention, as the engine automatically generates and attaches a stack trace to these objects. Throwing an Error Object (Standard):
throw new Error("Exception description");
throw new TypeError("Type mismatch");
Throwing Primitives and Custom Objects (Permitted but non-standard):
throw "String exception"; 
throw 404;                
throw true;               
throw { code: 1, message: "Custom object" }; 

Automatic Semicolon Insertion (ASI) Caveat

The throw statement is subject to JavaScript’s Automatic Semicolon Insertion (ASI) rules. No line terminator is permitted between the throw keyword and the expression. If a line break occurs, the engine inserts a semicolon immediately after throw, resulting in a SyntaxError because the throw statement strictly requires an expression. Invalid Syntax:
throw 
new Error("This throws a SyntaxError due to ASI");
Valid Syntax:
throw new Error(
  "This is valid because the expression starts on the same line"
);
Master JavaScript with Deep Grasping Methodology!Learn More