PHP检查抛出的异常类型 [英] PHP check thrown exception type

查看:192
本文介绍了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);
}

这篇关于PHP检查抛出的异常类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆