PHP 要抓 Try / Catch 的 Exception,想要抓最上層的,要怎麼抓呢?
- 註:以前都使用 Exception $e 就可以抓到,現在抓不到~
PHP Exception 最上層 Throwable exception
原來 PHP5 以前用 Exception $e 可以抓到,但是 PHP7 改了~ PHP7 要使用 Throwable $t 才能抓到~
程式暫時想要相容兩個版的的話,可以改用下述寫法:
try { // ... } catch (Exception $e) { // PHP5 catch... } catch (Throwable $t) { // PHP7+ catch... }
- 官方文件:PHP: Exceptions - Manual - (class Exception implements Throwable),下述摘錄自此篇:
- Lists of Throwable and Exception tree as of 7.2.0
- Error
- ArithmeticError
- DivisionByZeroError
- AssertionError
- ParseError
- TypeError
- ArgumentCountError
- ArithmeticError
- Exception
- ClosedGeneratorException
- DOMException
- ErrorException
- IntlException
- LogicException
- BadFunctionCallException
- BadMethodCallException
- DomainException
- InvalidArgumentException
- LengthException
- OutOfRangeException
- BadFunctionCallException
- PharException
- ReflectionException
- RuntimeException
- OutOfBoundsException
- OverflowException
- PDOException
- RangeException
- UnderflowException
- UnexpectedValueException
- SodiumException
- Error