> ## 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 Catch Clause

The `catch` clause in PHP is a control structure used in conjunction with a `try` block to intercept exceptions or errors thrown during execution. It executes only when an exception matching its specified type signature is propagated up the call stack from the associated `try` block.

```php theme={"dark"}
try {
    // Code that may throw an exception
} catch (ExceptionType $e) {
    // Exception handling logic
}
```

## Core Mechanics

**Type Resolution and Polymorphism**
The `catch` block specifies the class or interface of the exception it can handle. PHP evaluates this using `instanceof` semantics. If an exception is thrown, PHP checks if the thrown object is an instance of the type declared in the `catch` signature. Because of this polymorphic behavior, catching a base class or interface (such as `Throwable` or `Exception`) will implicitly catch all derived child classes.

**Evaluation Order**
When multiple `catch` blocks are chained, PHP evaluates them sequentially from top to bottom. The runtime executes the first block with a matching type signature and ignores subsequent blocks. Consequently, `catch` blocks must be ordered hierarchically from the most specific subclass to the most general superclass. Reversing this order results in unreachable code, as the general catch block will intercept the exception before the specific one is evaluated.

```php theme={"dark"}
try {
    // ...
} catch (ChildException $e) {
    // Evaluated first
} catch (ParentException $e) {
    // Evaluated second
} catch (Throwable $e) {
    // Evaluated last (catches anything implementing Throwable)
}
```

**Variable Binding and Scoping**
By default, the caught exception instance is bound to a specified variable (e.g., `$e`). In PHP, control structures do not create block-level scope. The exception variable is bound within the enclosing local scope (either the function scope or the global scope). It remains accessible even after the `catch` block has finished executing and will overwrite any previously defined variable with the identical name in that scope. This variable provides access to the exception's internal state and methods, such as `getMessage()`, `getCode()`, and `getTrace()`.

## Advanced Syntax Features

**Multi-Catch (PHP 7.1+)**
A single `catch` block can handle multiple, distinct exception types by separating the class names with a pipe (`|`) character. This prevents code duplication when different exception types require identical handling logic.

```php theme={"dark"}
try {
    // ...
} catch (InvalidArgumentException | OutOfBoundsException $e) {
    // Executes if either exception type is thrown
}
```

**Non-capturing Catches (PHP 8.0+)**
If the exception handling logic does not require access to the exception object itself, the variable binding can be omitted entirely. The `catch` block will still intercept the specified exception type. The exception object is still instantiated in memory when thrown, but it is simply not bound to a variable in the local symbol table.

```php theme={"dark"}
try {
    // ...
} catch (SpecificException) {
    // Intercepts SpecificException, but no variable is bound in the local scope
}
```

## Interaction with `finally`

A `catch` clause can optionally be followed by a `finally` block. If an exception is caught, the `catch` block executes first, followed immediately by the `finally` block. If a `catch` block throws a new exception (or re-throws the current one), the `finally` block will still execute before the new exception propagates further up the call stack.

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