Hierarchy of PHP exceptions
PHP is on its way to use more and more exceptions, and with PHP 8.0 throwing \TypeError
and \ValueError
exceptions, you will be seeing lots of exceptions working with modern PHP!
Here is a quick chart to help you take an overview look at the Exceptions used in PHP versions:
Throwable
├── Error
│ ├── ArithmeticError
│ │ └── DivisionByZeroError
│ ├── AssertionError
│ ├── CompileError
│ │ └── ParseError
│ ├── TypeError
│ │ └── ArgumentCountError
│ ├── ValueError
│ ├── UnhandledMatchError
| ├── FiberError
| └── Random\RandomError
│ └── BrokenRandomEngineError
└── Exception
├── ClosedGeneratorException
├── DOMException
├── ErrorException
├── IntlException
├── JsonException
├── LogicException
│ ├── BadFunctionCallException
│ │ └── BadMethodCallException
│ ├── DomainException
│ ├── InvalidArgumentException
│ ├── LengthException
│ └── OutOfRangeException
├── PharException
├── ReflectionException
├── RuntimeException
│ ├── OutOfBoundsException
│ ├── OverflowException
│ ├── PDOException
│ ├── RangeException
│ ├── UnderflowException
│ ├── UnexpectedValueException
│ └── PDOException
├── SodiumException
├── FiberExit
└── Random\RandomException
Notes
- All of these exceptions are in the global namespace.
\SodiumException
is from thesodium
extension.\PDOException
is from thepdo
extension.\RandomException
andRandomError
are from theRandom
extension added in PHP 8.2.- The list is not exhaustive. Additional extensions might add more exceptions, but you can catch them all by catching
\Throwable
, or by the more specific\Error
or\Exception
classes.
Changes
PHP 8.0
\ValueError
is new in PHP 8.0.\UnhandledMatchError
is new in PHP 8.0.
PHP 8.1
\FiberError
and\FiberExit
are new in PHP 8.1.