What Exception subclasses are built into PHP?

PHP 5 has two built in exceptions

Libraries within PHP have their own built in exceptions

  • DOMException DOM operations raise exceptions under particular circumstances, i.e., when an operation is impossible to perform for logical reasons.
  • IntlException his class is used for generating exceptions when errors occur inside intl functions. Such exceptions are only generated when intl.use_exceptions is enabled.
  • PharException Thrown when working with the Phar class
  • ReflectionException Thrown when working with Reflection classes

SPL includes a few of its own built in exceptions:

PHP 7 introduces new exceptions including catchable errors. New exceptions include:

  • Throwable is the base interface for any object that can be thrown via a throw statement in PHP 7, including Error and Exception.
  • Error is the base class for all internal PHP errors.
  • AssertionError is thrown when an assertion made via assert() fails.
  • ParseError is thrown when an error occurs while parsing PHP code, such as when eval() is called.
  • TypeError There are three scenarios where a TypeError may be thrown. The first is where the argument type being passed to a function does not match its corresponding declared parameter type. The second is where a value being returned from a function does not match the declared function return type. The third is where an invalid number of arguments are passed to a built-in PHP function (strict mode only).
  • ArithmeticError is thrown when an error occurs while performing mathematical operations. In PHP 7.0, these errors include attempting to perform a bitshift by a negative amount, and any call to intdiv() that would result in a value outside the possible bounds of an integer.
  • DivisionByZeroError is thrown when an attempt is made to divide a number by zero.
  • ArgumentCountError is thrown when too few arguments are passed to a user-defined function or method.

PHP 7.3 introduces JSON exceptions:

  • JsonException is thrown when json_encode() and json_decode() experience an error.

PHP 8 introduces one new exception:

  • ValueError is thrown when you pass a value to a function, which has a valid type but can not be used for the operation.

PHP 8.3 will add new exceptions for Date/Time errors

Here’s a chart that demonstrates the new hierarchy introduced in PHP 7:

\Throwable
├── \Exception (implements \Throwable)
|   |── \DOMException (extends \Exception)
|   ├── \IntlException (extends \Exception)
|   ├── \JsonException (extends \Exception)
|   |── \PharException (extends \Exception)
|   |── \ReflectionException (extends \Exception)
|   |── \ValueError (extends \Exception)
│   ├── \LogicException (extends \Exception)
│   │   ├── \BadFunctionCallException (extends \LogicException)
│   │   │   └── \BadMethodCallException (extends \BadFunctionCallException)
│   │   ├── \DomainException (extends \LogicException)
│   │   ├── \InvalidArgumentException (extends \LogicException)
│   │   ├── \LengthException (extends \LogicException)
│   │   └── \OutOfRangeException (extends \LogicException)
│   └── \RuntimeException (extends \Exception)
│       ├── \OutOfBoundsException (extends \RuntimeException)
│       ├── \OverflowException (extends \RuntimeException)
│       ├── \RangeException (extends \RuntimeException)
│       ├── \UnderflowException (extends \RuntimeException)
│       └── \UnexpectedValueException (extends \RuntimeException)
└── \Error (implements \Throwable)
    ├── \AssertionError (extends \Error)
    ├── \ParseError (extends \Error)
    └── \TypeError (extends \Error)
        └── \ArgumentCountError (extends \TypeError)
    └── \ArithmeticError (extends \Error)
        └── \DivisionByZeroError extends \ArithmeticError)

Leave a Comment