PHP 5 has two built in exceptions
ExceptionErrorException
Libraries within PHP have their own built in exceptions
DOMExceptionDOM operations raise exceptions under particular circumstances, i.e., when an operation is impossible to perform for logical reasons.IntlExceptionhis class is used for generating exceptions when errors occur inside intl functions. Such exceptions are only generated when intl.use_exceptions is enabled.PharExceptionThrown when working with the Phar classReflectionExceptionThrown when working with Reflection classes
SPL includes a few of its own built in exceptions:
BadFunctionCallExceptionA callback refers to an undefined function or if some arguments are missing.BadMethodCallExceptionA callback refers to an undefined method or if some arguments are missing.DomainExceptionA value does not adhere to a defined valid data domain.InvalidArgumentExceptionThe arguments passed were invalid.LengthExceptionThe parameter exceeds the allowed length (used for strings, arrays, file size, etc.).LogicExceptionGeneric error occurred in program logic.OutOfBoundsExceptionAn illegal index was requested.OutOfRangeExceptionAn illegal index was requested. This represents errors that should be detected at compile time.OverflowExceptionAdding an element to a full container.RangeExceptionIndicate range errors during program execution. Normally this means there was an arithmetic error other than under/overflow.RuntimeExceptionAn error which can only be found on runtime occurs.UnderflowExceptionPerforming an invalid operation on an empty container, such as removing an element.UnexpectedValueExceptionAn unexpected value was received (i.e. as the result of a returned value from a method call).
PHP 7 introduces new exceptions including catchable errors. New exceptions include:
Throwableis the base interface for any object that can be thrown via a throw statement in PHP 7, including Error and Exception.Erroris the base class for all internal PHP errors.AssertionErroris thrown when an assertion made via assert() fails.ParseErroris thrown when an error occurs while parsing PHP code, such as when eval() is called.TypeErrorThere 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).ArithmeticErroris 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.DivisionByZeroErroris thrown when an attempt is made to divide a number by zero.ArgumentCountErroris thrown when too few arguments are passed to a user-defined function or method.
PHP 7.3 introduces JSON exceptions:
JsonExceptionis thrown whenjson_encode()andjson_decode()experience an error.
PHP 8 introduces one new exception:
ValueErroris 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)