加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

里面的PHP异常catch:如何处理呢?

发布时间:2020-12-13 18:02:56 所属栏目:PHP教程 来源:网络整理
导读:假设在try … catch块中有一个 PHP代码.假设你想要做内部捕获(即发送电子邮件)可能会失败并抛出新的异常. try { // something bad happens throw new Exception('Exception 1');}catch(Exception $e) { // something bad happens also here throw new Except
假设在try … catch块中有一个 PHP代码.假设你想要做内部捕获(即发送电子邮件)可能会失败并抛出新的异常.
try {
    // something bad happens
    throw new Exception('Exception 1');
}
catch(Exception $e) {
    // something bad happens also here
    throw new Exception('Exception 2');
}

在catch块中处理异常的正确(最佳)方法是什么?

你不应该把任何东西扔进去.如果你这样做,那么你可以省略try-catch的这个内层并在try-catch的外层捕获异常并在那里处理那个异常.

例如:

try {
    function(){
        try {
            function(){
                try {
                    function (){}
                } catch {
                    throw new Exception("newInner");
                }
            }
        } catch {
            throw new Exception("new");
        }
    }
} catch (Exception $e) {
    echo $e;
}

可以替换为

try {
    function(){
        function(){
            function (){
                throw new Exception("newInner");
            }
        }
    }
} catch (Exception $e) {
    echo $e;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读