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

# PHP Throw Statement

The `throw` construct in PHP is used to halt normal program execution and signal an exceptional condition by emitting an object that implements the `Throwable` interface. When executed, the PHP engine immediately stops evaluating the current code block and transfers control to the exception handling mechanism to locate an appropriate `catch` block capable of handling the emitted object's specific type.

## Syntax

```php theme={"dark"}
throw <expression>;
```

The `<expression>` must evaluate to an instance of a class that implements the `Throwable` interface. In PHP, this is typically an instance of `Exception`, `Error`, or a user-defined subclass of either.

```php theme={"dark"}
// Standard syntax instantiating an exception object
throw new Exception('Descriptive error message', 100);
```

## Technical Mechanics

1. **Type Restriction:** Attempting to throw a scalar value (string, integer), an array, or an object that does not implement `Throwable` (such as `stdClass`) will result in a `TypeError` exception. This `TypeError` is fully catchable (e.g., `catch (TypeError $e)`) and only results in a Fatal Error if it remains uncaught.
2. **Control Flow Interruption & Stack Unwinding:** Any code immediately following a `throw` execution is strictly unreachable. The engine transfers control to the nearest matching `catch` block.
   * **Local Handling:** If the `throw` occurs within a `try` block that has a matching `catch` in the same scope, control transfers directly to that `catch` block, and any associated `finally` block executes *afterward*.
   * **Stack Unwinding:** If the exception is unhandled locally, the engine begins unwinding the call stack to propagate the exception to a higher scope. During this unwinding process, any `finally` blocks attached to the `try` scopes being completely exited will execute *before* the exception reaches a `catch` block further up the call stack.
3. **Uncaught Exceptions:** If the engine unwinds the entire call stack without finding a `catch` block that matches the thrown object's type, it triggers an Uncaught Exception. If a global exception handler has been registered via `set_exception_handler()`, it will intercept the exception. While the handler executes, execution cannot resume where the exception was thrown, and the script will still terminate after the handler finishes. If no handler is registered, the uncaught exception results in a Fatal Error and immediate script termination.
4. **Exception Re-throwing:** A `throw` construct can be used inside a `catch` block to re-emit the caught exception (or a new one) further up the call stack.

```php theme={"dark"}
try {
    // Code that may throw
} catch (SpecificException $e) {
    // Re-throwing the exact same exception object
    throw $e; 
}
```

## `throw` as an Expression (PHP 8.0+)

Prior to PHP 8.0, `throw` was strictly a statement, meaning it could not be used in contexts where a value was expected. As of PHP 8.0, `throw` is an expression. This architectural change allows it to be evaluated in single-expression contexts, such as arrow functions, ternary operators, and the null coalescing operator.

**Null Coalescing Operator Context:**

```php theme={"dark"}
$value = $nullableValue ?? throw new InvalidArgumentException('Value cannot be null');
```

**Ternary Operator Context:**

```php theme={"dark"}
$result = $condition ? 'Success' : throw new RuntimeException('Condition failed');
```

**Arrow Function Context:**

```php theme={"dark"}
$closure = fn($x) => $x > 0 ? $x : throw new DomainException('Must be positive');
```

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