异常处理

<?php
    //异常处理
    
    //固定格式
    try{
        echo 1;
        throw new Exception("Error Processing Request", 1);
        echo 2;
    }catch(Exception $e){
        echo $e;
        echo "<br>";
        echo $e -> getMessage();
        echo "<br>";
        echo $e -> getCode();
    }

    /*
         * 注意:try catch之间不能加任何代码
         * 注意:try-catch是一种结构,一个try至少对应一个catch
    */
?>

自定义异常处理类

<?php
    class myException extends Exception
    {
        function demo(){
            echo "执行第二套方案<br>";
        }
    }

    try{
        echo "love<br>";
        throw new myException("黑恶黑呵呵呵");
        echo "you<br>";
        
    }catch(myException $e){
        echo $e;
        echo "<br>";
        echo $e->getMessage();
        echo "<br>";
        echo $e->getCode();
        echo "<br>";
        $e -> demo();
    }catch(Exception $e){
        echo $e;
        echo "<br>";
        echo $e->getMessage();
        echo "<br>";
        echo $e->getCode();
        echo "<br>";
        $e -> demo();
    }
?>
posted @ 2020-01-12 14:35  初雨诗清风  阅读(103)  评论(0编辑  收藏  举报