6

I think in all programming languages Exception class is instance of Throwable interface.

Take a look at following code which shows Exception is not instance of Throwable in php.

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
    }

}

It makes problem with chaining exceptions where Exception class constructor accepts Throwable in it's last argument.

php version: 5.6.23

Any solution?

Styphon
  • 10,304
  • 9
  • 52
  • 86
Lost Koder
  • 864
  • 13
  • 32

1 Answers1

12

Throwable is the base interface for any object that can be thrown via a throw statement in PHP 7, including Error and Exception. And your code produces: $e is exception $e is throwable if you have PHP version >= 7

But you have PHP version 5.6.23, so Throwable interface is not available for this version.

wp78de
  • 18,207
  • 7
  • 43
  • 71
arbogastes
  • 1,308
  • 9
  • 10
  • 1
    Had this problem while thinking I am on php7, but actually php7 was installed, but the 5.6 configured in the config file... luckly your post made me check that twice before searching for more. – Cagatay Ulubay Jul 26 '17 at 07:21