Catching multiple exception types in one catch block

Catching multiple exception types in one catch block

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Catching Multiple Exception Types in One Block

In many languages, handling specific errors requires writing verbose catch blocks. You might find yourself repeating identical logic for several exception types that share a common hierarchy. PHP's standard try-catch syntax does not support multiple exception classes in a single catch declaration like you might expect from other languages or pseudocode examples.

A common misconception is that a shared base class allows for a unified catch block. If AError and BError both extend an abstract base class, simply catching that base class will also handle any other exceptions inheriting from it. This breaks the principle of least surprise if you have a fallback handler intended only for unrelated errors.

To address this specific scenario—catching AError and BError together while letting everything else fall through to a generic handler—you must rely on type checking within the catch block rather than the declaration itself.

The Standard Approach: Separate Blocks or Type Checks

The most straightforward method in standard PHP is to define separate catch blocks for each specific exception. This keeps the code explicit and readable, but it can become repetitive if you have many similar types.

try {
    // Something that might fail
} catch (AError $e) {
    handler1($e);
} catch (BError $e) {
    handler1($e);
} catch (Exception $e) {
    handler2($e);
}

While this works, it violates DRY principles if the handling logic is identical. A more elegant solution involves a single catch block for the known specific exceptions and a second block for everything else. Inside the first block, you can determine which specific type occurred.

Using instanceof to Unify Logic

You can consolidate the handling of AError and BError by catching them individually in separate blocks but extracting the common logic into a shared function, or by catching one and checking for the other. However, PHP's catch statement does not allow a union type like catch (AError | BError $e) until version 8.0 introduced union types to the language syntax.

If you are running PHP 8.0 or later, you can actually catch multiple specific classes in a single block using the union syntax:

try {
    // Something that might fail
} catch (AError|BError $e) {
    handler1($e);
} catch (Exception $e) {
    handler2($e);
}

This is the cleanest modern solution. It satisfies your requirement for a single block while maintaining strict type safety. The interpreter determines at runtime whether the thrown object matches AError, BError, or both, ensuring that unrelated exceptions fall through to the final Exception catch.

If you are on PHP 7.x, this syntax is invalid. In that environment, you must use the multiple-catch-block pattern shown earlier. Alternatively, you can catch a broader base class if it exists, but only if you are certain no other unrelated classes inherit from it. Since your requirement explicitly states that AError and BError share a base class with other types that must bypass the handler, catching the parent class is not an option.

Practical Considerations for Error Handling

When designing this structure, consider the order of your catch blocks. PHP evaluates them top-to-bottom. If you place a broad Exception catch before specific ones, it will intercept all errors, rendering the specific handlers useless. Always list specific exceptions first.

Another consideration is logging. If handler1 logs the error, ensure it doesn't suppress the original stack trace. The $e variable in the union type block retains full context of which specific class was thrown, allowing you to distinguish between AError and BError if necessary:

if ($e instanceof AError) {
    log('AError occurred', $e);
} elseif ($e instanceof BError) {
    log('BError occurred', $e);
} else {
    // Should not happen in this specific block, but good for safety
    log('Unknown error type in handler1', $e);
}

This pattern ensures that even with unified handling, you retain the granularity needed for debugging. For teams using modern PHP stacks, leveraging the union syntax reduces boilerplate without sacrificing readability or control.

Our network