PHP 5有两个内置的例外
Exception
ErrorException
SPL包括一些自己的内置例外 :
BadFunctionCallExceptioncallback指的是一个未定义的函数,或者缺less一些参数。
BadMethodCallExceptioncallback指的是一个未定义的方法,或者是缺less一些参数。
DomainException值不符合已定义的有效数据域。
InvalidArgumentException传递的参数无效。
LengthException参数超出允许的长度(用于string,数组,文件大小等)。
LogicException程序逻辑中发生一般错误。
OutOfBoundsException请求了非法索引。
OutOfRangeException请求了非法索引。 这表示应在编译时检测到的错误。
OverflowException将元素添加到完整的容器。
RangeException在程序执行期间指示范围错误。 通常这意味着除了溢出/溢出之外,还有一个算术错误。
RuntimeException发生只能在运行时发现的错误。
UnderflowException在空容器上执行无效操作,例如删除元素。
UnexpectedValueException收到意外的值(即作为方法调用的返回值的结果)。
PHP 7引入了新的exception,包括可捕获的错误。 新的例外包括:
Throwable是可以通过PHP 7中的throw语句引发的任何对象的基本接口,包括Error和Exception。
Error是所有内部PHP错误的基类。
当通过assert()断言失败时, AssertionError被抛出。
在parsingPHP代码时发生错误时会引发ParseError ,例如调用eval()时。
TypeError有三种情况可能会引发TypeError。 首先是传递给函数的参数types与其相应的声明参数types不匹配。 第二个是从函数返回的值与声明的函数返回types不匹配。 第三个是无效数量的parameter passing给一个内置的PHP函数(仅限严格模式)。
在执行math运算时发生错误时,会引发ArithmeticError 。 在PHP 7.0中,这些错误包括尝试执行一个负数的负移位,以及对intdiv()的任何调用,这会导致整数的可能范围之外的值。
如果尝试将数字除以零,则会引发DivisionByZeroError 。
当太less的parameter passing给用户定义的函数或方法时,会引发ArgumentCountError 。
下面的图表演示了PHP 7中引入的新层次结构:
\Throwable ├── \Exception (implements \Throwable) │ ├── \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)