错误处理

集中成功优酷在代码块中发生的异常

在代码块中发生了异常直接抛出,代码块中不处理异常,将异常集中起来一起处理

使用的关键字

  try:检测代码块

  catch:捕获异常

  throw:抛出异常

  finally:无论有无异常都会执行,可以省略

  Exception:异常类

在文档中Exception 类中有属性和方法

语法结构

  

<?php 

//这里可以写php代码了

    try{
        //检测代码
    }catch(Exception $e){
        //捕获异常
    }
    finally{
        //不论是否有异常,都要执行,finally可以省略
    }

?>

例题

  

<?php         
        if(isset($_POST['btn'])){
            
            try {
                $age=$_POST['age'];
                if($age==''){
                    //抛出异常
                    throw new Exception("年龄不能为空",1001);
                }
                if(!is_numeric($age)){
                    throw new Exception("年龄必须是数字");
                }
                if(!($age >=10 && $age <=35)){
                    throw new Exception("年龄必须要在10-35之间",1002);
                    echo '您的年龄'.$age;
                }
                //捕获异常
                } catch (Exception $e) {
                    echo "错误信息".$e->getMessage()."<br>";
                    echo "错误代码".$e->getCode().'<br>';
                    echo "错误行号".$e->getLine().'<br>';
                    echo "错误文件".$e->getFile().'<br>';
            }
    }
    
    ?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <form action="" method="POST">
        年龄: <input type="text" name="age" id=""> <br/>
        <input type="submit" name="btn" value="提交">
    </form>
</body>
</html>

 注意:抛出异常以后,try块终止执行,执行权限交给了catch

自定义异常

场景:如果实现异常的分类处理?比如异常有三个级别异常对应三种处理方式

自定义一场即可

所有异常类的父类是Exception,Exception中的方法不允许重写

<?php
    //自定义空异常类
    class MyNull extends Exception{
        //继承 异常类就行,异常类中的方法都是final  不能不重写
    }

    //自定义类型异常类
    class MyType extends Exception{

    }

    //自定义范围异常类
    class MyRange extends Exception{

    }

    //逻辑代码


    if(isset($_POST['btn'])){
        try {
                $age=$_POST['age'];
                $name=$_POST['name'];
                if($name==''){
                    //抛出异常
                    throw new MyNull("姓名不能为空");
                }
                if($age==''){
                    //抛出异常
                    throw new MyNull("年龄不能为空");
                }

                if(!is_numeric($age)){
                    throw new MyType("年龄只能是数字");
                }

                if(!($age >10 && $age <35)){
                    throw new MyRange("年龄只能在10之35岁之间");
                }

        } catch (MyType $e) {
            
            echo 'mytype 异常类 给你提示'.$e->getMessage();

        }catch (MyNull $e){

            echo 'mynull 异常类 给你提示'.$e->getMessage();

        }catch (MyRange $e ){

            echo 'myrange 异常类 给你提示'.$e->getMessage();

        }


    }

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="" method="post">
        姓名:<input type="text" name="name" id=""> <br>
        年龄:<input type="text" name="age" id=""> <br>
        <input type="submit" value="提交" name="btn">
    
    </form>
</body>
</html>
<?php
    //自定义空异常类
    class MyNull extends Exception{
        //继承 异常类就行,异常类中的方法都是final  不能不重写
    }

    //自定义类型异常类
    class MyType extends Exception{

    }

    //自定义范围异常类
    class MyRange extends Exception{

    }

    //逻辑代码


    if(isset($_POST['btn'])){
        try {
                $age=$_POST['age'];
                $name=$_POST['name'];
                if($name==''){
                    //抛出异常
                    throw new MyNull("姓名不能为空");
                }
                if($age==''){
                    //抛出异常
                    throw new MyNull("年龄不能为空");
                }

                if(!is_numeric($age)){
                    throw new MyType("年龄只能是数字");
                }

                if(!($age >10 && $age <35)){
                    throw new MyRange("年龄只能在10之35岁之间");
                }

        } catch (MyType $e) {
            
            echo 'mytype 异常类 给你提示'.$e->getMessage();

        }catch (MyNull $e){

            echo 'mynull 异常类 给你提示'.$e->getMessage();

        }catch (MyRange $e ){

            echo 'myrange 异常类 给你提示'.$e->getMessage();

        }


    }

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="" method="post">
        姓名:<input type="text" name="name" id=""> <br>
        年龄:<input type="text" name="age" id=""> <br>
        <input type="submit" value="提交" name="btn">
    
    </form>
</body>
</html>
原文地址:https://www.cnblogs.com/xiaowie/p/14276531.html