异常处理

php
    //异常处理
    
    //固定格式
    try{
        echo 1;
        throw new Exception("Error Processing Request", 1);
        echo 2;
    }catch(Exception $e){
        echo $e;
        echo "
"; echo $e -> getMessage(); echo "
"; echo $e -> getCode(); } /* * 注意:try catch之间不能加任何代码 * 注意:try-catch是一种结构,一个try至少对应一个catch */ ?>

自定义异常处理类

php
    class myException extends Exception
    {
        function demo(){
            echo "执行第二套方案
"; } } try{ echo "love
"; throw new myException("黑恶黑呵呵呵"); echo "you
"; }catch(myException $e){ echo $e; echo "
"; echo $e->getMessage(); echo "
"; echo $e->getCode(); echo "
"; $e -> demo(); }catch(Exception $e){ echo $e; echo "
"; echo $e->getMessage(); echo "
"; echo $e->getCode(); echo "
"; $e -> demo(); } ?>

你可能感兴趣的:(异常处理)