> ## 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.

# JavaScript Throw Statement

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

```javascript theme={"dark"}
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):**

```javascript theme={"dark"}
throw new Error("Exception description");
throw new TypeError("Type mismatch");
```

**Throwing Primitives and Custom Objects (Permitted but non-standard):**

```javascript theme={"dark"}
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:**

```javascript theme={"dark"}
throw 
new Error("This throws a SyntaxError due to ASI");
```

**Valid Syntax:**

```javascript theme={"dark"}
throw new Error(
  "This is valid because the expression starts on the same line"
);
```

<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 JavaScript 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>
