PHP检查抛出的异常类型

问题描述:

当然,在PHP中,您可以使用以下命令捕获所有引发的异常:

Of course in PHP you can catch all thrown exceptions with:

try{
    /* code with exceptions */
}catch(Exception $e) {
    /* Handling exceptions */
}

但是有没有办法从catch块内部检查抛出的异常的异常类型?

But is there a way to check the exception type of the thrown exception from inside the catch block?

您可以使用 get_class

You can use get_class:

try {
    throw new InvalidArgumentException("Non Sequitur!", 1);
} catch (Exception $e) {
    echo get_class($e);
}