为什么 Exception 不是 Throwable 的实例?

Why Exception is not instance of Throwable?

我认为在所有编程语言中 Exception class 是 Throwable 接口的实例。

看看下面的代码,它显示 Exception 不是 php 中 Throwable 的实例。

try {

    throw new InvalidArgumentException("error message");

} catch (InvalidArgumentException $e) {

    if ($e instanceof Exception) {
        echo '$e is exception';             // this line gets executed
    }

    if ($e instanceof Throwable) {
        echo '$e is throwable';             // but this one never
    }

}

Exception class 构造函数在其最后一个参数中接受 Throwable 的链接异常会产生问题。

php版本:5.6.23

有什么解决办法吗?

Throwable 是可以通过 PHP 7 中的 throw 语句抛出的任何对象的基本接口,包括 ErrorException。你的代码会产生: $e is exception $e is throwable 如果你有 PHP version >= 7

但是您有 PHP 版本 5.6.23,因此 Throwable 界面 不适用于此版本.