【问题标题】:Why Exception is not instance of Throwable?为什么异常不是 Throwable 的实例?
【发布时间】:2017-05-02 16:30:30
【问题描述】:

我认为在所有编程语言中Exception 类都是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 类构造函数的最后一个参数中接受 Throwable 时,链式异常会出现问题。

php版本:5.6.23

有什么办法吗?

【问题讨论】:

标签: php oop php-5.6


【解决方案1】:

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

但是你有PHP版本5.6.23,所以Throwable接口这个版本不可用

【讨论】:

  • 以为我在php7上时遇到了这个问题,但实际上安装了php7,但配置文件中配置了5.6......幸运的是你的帖子让我在搜索更多之前检查了两次。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-12
  • 2013-03-13
  • 2016-10-26
  • 2010-10-03
相关资源
最近更新 更多