What Exception subclasses are built into PHP?
I have not yet been able to find a list of all the built-in Exception sub classes in PHP. I'd rather use built in ones when they make sense, before creating my own exception subclasses.
For example, I know InvalidArgumentException exists, but there appears to be nothing comparable to Java's NullPointerException.
Does anyone have or can link to a list of the available Exception subclasses in PHP?
PHP 5 has two built in exceptions
Exception
ErrorException
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:
-
BadFunctionCallException
A callback refers to an undefined function or if some arguments are missing. -
BadMethodCallException
A callback refers to an undefined method or if some arguments are missing. -
DomainException
A value does not adhere to a defined valid data domain. -
InvalidArgumentException
The arguments passed were invalid. -
LengthException
The parameter exceeds the allowed length (used for strings, arrays, file size, etc.). -
LogicException
Generic error occurred in program logic. -
OutOfBoundsException
An illegal index was requested. -
OutOfRangeException
An illegal index was requested. This represents errors that should be detected at compile time. -
OverflowException
Adding an element to a full container. -
RangeException
Indicate range errors during program execution. Normally this means there was an arithmetic error other than under/overflow. -
RuntimeException
An error which can only be found on runtime occurs. -
UnderflowException
Performing an invalid operation on an empty container, such as removing an element. -
UnexpectedValueException
An 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:
-
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 whenjson_encode()
andjson_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.
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)
Actually, I managed to find them now that I broke down and asked.
Technically it looks like PHP only has 2 built in exceptions: http://www.php.net/manual/en/reserved.exceptions.php
And the SPL Defines several more: http://www.php.net/manual/en/spl.exceptions.php
With an explanation of their heirarchy:
http://www.php.net/~helly/php/ext/spl/classException.html
http://web.archive.org/web/20130829124146/http://www.php.net/~helly/php/ext/spl/classException.html (archived on 29 Aug 2013)
Update
The link above is dead, it was a diagram of the basic PHP exception hierarchy. I couldn't find the original, but here's a replacement:
Originally found at http://fossies.org/dox/php-5.2.17/classException.html, now archived at http://web.archive.org/web/20151002165210/https://fossies.org/dox/php-5.2.17/classException.html (archived on 02 Oct 2015)