All articles

Find and fix PHP problems

Should a Custom PHP Exception Extend Exception or an SPL Subclass?

Choose a parent for your custom PHP exception, read the Throwable hierarchy, and check a RuntimeException subclass in a stock example.

Editorial illustration for Should a Custom PHP Exception Extend Exception or an SPL Subclass?

Extend Exception or a subclass that describes the failure. Use LogicException for a program-logic failure, RuntimeException for a failure discovered during operation, and plain Exception when neither distinction helps. Do not try to implement Throwable directly for a custom exception. The parent matters because it tells readers what sort of failure the class represents and determines its place in PHP's exception hierarchy.

Read the two sides of Throwable

In PHP 7 and later, Exception and Error both implement Throwable. They are separate branches, so an Error is not an Exception. A custom application exception belongs on the Exception side: a user-defined exception class cannot implement Throwable directly, while extending Exception makes it a Throwable through inheritance. See the PHP exception hierarchy for the wider tree.

The portion relevant to this decision looks like this:

Throwable
├── Error
└── Exception
    ├── ErrorException
    ├── LogicException
    │   ├── BadFunctionCallException
    │   │   └── BadMethodCallException
    │   ├── DomainException
    │   ├── InvalidArgumentException
    │   ├── LengthException
    │   └── OutOfRangeException
    └── RuntimeException
        ├── OutOfBoundsException
        ├── OverflowException
        ├── RangeException
        ├── UnderflowException
        └── UnexpectedValueException

ErrorException is on the Exception side despite its name. The two named groups beneath LogicException and RuntimeException are the SPL exception branches useful for choosing a more specific parent. The PHP manual's reserved-exceptions page also points readers to SPL exceptions. The wider hierarchy includes PDOException beneath RuntimeException; it is not necessary to make every operational failure a PDOException.

The hierarchy can change as PHP adds types. For example, ValueError and UnhandledMatchError are listed as new in PHP 8.0. Do not add them to the SPL branches above merely because they are built-in types. When identifying an unfamiliar exception, check its actual parent rather than guessing from the last word in its name.

Match the parent to the failure

Choose LogicException when the exception represents an error in program logic. Its descendants let you be more precise when one fits: InvalidArgumentException, for instance, describes an argument that is not of the expected type. A custom subclass is useful when that existing name does not identify your particular failure clearly enough.

Choose RuntimeException when the failure becomes apparent during operation. In the stock example below, a request for five items encounters only three available items. That condition concerns whether this particular request can be fulfilled; the example does not use the exception to label a mistake in the program's logic. InsufficientStockException therefore names the specific failure while retaining RuntimeException as its parent.

If the logic-versus-operation distinction would make your exception misleading rather than clearer, extend Exception directly. That still gives the class a valid place under Throwable. A more specific parent is useful when its meaning fits, not simply because it exists.

Check a custom RuntimeException subclass

This self-contained example uses no extension or external class. The subclass adds no methods because its name and parent provide the distinction needed here. The catch block only displays its relationships; it is not a guide to where an application should handle the failure.

<?php

class InsufficientStockException extends RuntimeException
{
}

$available = 3;
$requested = 5;

try {
    if ($requested > $available) {
        throw new InsufficientStockException('Not enough stock for this request.');
    }
} catch (InsufficientStockException $failure) {
    var_dump($failure instanceof RuntimeException);
    var_dump($failure instanceof Exception);
    var_dump($failure instanceof Throwable);
}

Each check prints bool(true): the custom class extends RuntimeException, which sits beneath Exception, and Exception implements Throwable. Its immediate parent communicates an operational failure, while its own name identifies the failed stock request. If you were instead naming an error in program logic, the same hierarchy would point you toward LogicException.

← All articles