Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 879
CLo Avatar asked May 31 '12 17:05

CLo


People also ask

What are types exception in PHP?

An exception is an object that describes an error or unexpected behaviour of a PHP script. Exceptions are thrown by many PHP functions and classes. User defined functions and classes can also throw exceptions. Exceptions are a good way to stop a function when it comes across data that it cannot use.

What are the subclasses of exception class?

The Exception class has two main subclasses: IOException class and RuntimeException Class.

What is custom exception in PHP?

The customException() class is created as an extension of the old exception class. This way it inherits all methods and properties from the old exception class. The errorMessage() function is created. This function returns an error message if an e-mail address is invalid.

Does exception catch all exceptions PHP?

Catching all PHP exceptions Because exceptions are objects, they all extend a built-in Exception class (see Throwing Exceptions in PHP), which means that catching every PHP exception thrown is as simple as type-hinting the global exception object, which is indicated by adding a backslash in front: try { // ... }


1 Answers

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 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.

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) 
like image 123
John Conde Avatar answered Sep 20 '22 09:09

John Conde