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?
Throwable is a class which Exception – and consequently all subclasses thereof – subclasses. There's nothing stopping you from using instanceof on a Throwable .
The class at the top of the exception class hierarchy is the Throwable class, which is a direct subclass of the Object class. Throwable has two direct subclasses - Exception and Error. The Exception class is used for exception conditions that the application may need to handle.
Syntax: throw Instance Example: throw new ArithmeticException("/ by zero"); But this exception i.e, Instance must be of type Throwable or a subclass of Throwable. For example Exception is a sub-class of Throwable and user defined exceptions typically extend Exception class.
The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With