PHP 7 错误处理

PHP 7 改变了大多数错误的报告方式。不同于 PHP 5 的传统错误报告机制,现在大多数错误 被作为 Error 异常抛出。

这种 Error 异常可以像普通异常一样被 try / catch 块所捕获。如果没有匹配的 try / catch 块, 则调用异常处理函数(由 set_exception_handler() 注册)进行处理。 如果尚未注册异常处理函数,则按照传统方式处理:被报告为一个致命错误(Fatal Error)。

Error 类并不是从 Exception 类 扩展出来的,所以用 catch (Exception $e) { ... } 这样的代码是捕获不 到 Error 的。你可以用 catch (Error $e) { ... } 这样的代码,或者通过注册异常处理函数( set_exception_handler())来捕获 Error

Error 异常层次结构

  • Throwable
    • Error
      • ArithmeticError
      • AssertionError
      • DivisionByZeroError
      • ParseError
      • TypeError
    • Exception
      • ...
add a note

User Contributed Notes 3 notes

up
18
hungry dot rahly at gmail dot com
4 months ago
You can catch both exceptions and errors by catching(Throwable)
up
0
john at lariat dot co
7 hours ago
Congratulations!

PHP just implemented the same exception hierarchy that Java has had since 1996!!!! What took you guys so long? :P
up
0
Debo
22 hours ago
That's actually correct and valid only for PHP 7.