首页 > 其他 > 详细

try使用多个catch捕获不同的异常

时间:2020-05-10 14:24:19      阅读:206      评论:0      收藏:0      [点我收藏+]

目的:

使用多个catch捕获不同的异常

eg:

<?php
//创建三个Exception
class AException extends Exception{
    function printMsg(){
        echo "This is AException.";
    }
}
class BException extends Exception{
    function printMsg(){
        echo "This is BException.";
    }
}class CException extends Exception{
    function printMsg(){
        echo "This is CException.";
    }
}


//一个try,多个catch捕获不同的异常
try{
    $flag = 1;
    switch ($flag)
    {
        case 1:
            throw new AException(‘AException:‘);
            break;
        case 2:
            throw new BException(‘BException:‘);
            break;
        case 3:
            throw new CException(‘CException:‘);
            break;
        default:
            break;
    }
}
catch(AException $e){
    echo $e->getmessage();
    $e->printMsg();
}
catch(BException $e){
    echo $e->getmessage();
    $e->printMsg();
}
catch(CException $e){
    echo $e->getmessage();
    $e->printMsg();
}
catch(Exception $e){
    //这个catch主要是捕获漏网之鱼
    echo $e->getmessage();
}

 

输出:

AException:This is AException.

 

try使用多个catch捕获不同的异常

原文:https://www.cnblogs.com/lyc94620/p/12862794.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!