zoukankan      html  css  js  c++  java
  • PHP错误与异常

    错误报告关闭和打开

    php.ini 的 display_errors = On 或者 Off

    代码里 ini_set(‘display_errors’,1) 或者 0


    错误报告级别

    最佳实践

    开发环境下打开错误报告。并且错误报告级别 E_ALL
    正式环境一定要关闭错误报告

    //显示所有的错误类型
    error_reporting(E_ALL);
    
    //显示所有的错误类型,除了NOTICE 提示之外
    error_reporting(E_ALL ^ E_NOTICE);
    error_reporting(E_ALL &~ E_NOTICE);
    
    //关闭所有的PHP错误报告
    error_reporting(0);
    
    //报告所有的错误类型
    error_reporting(-1);

    举例

    try {
        //找不到文件
        throw new Exception('找不到文件',1);
        //没有权限访问
        throw new Exception('没有权限',2);
    } catch (Exception $e) {
        $errno =  $e -> getCode();
    
        if($errno == 1){
            echo $e -> getFile();
        }elseif($errno == 2){
            echo $e -> getLine();
        }
    }

    文件异常类

    class FileException extends Exception{
        public function fileInfo(){
            return $this->getMessage();
        }
    }
    
    try {
        print "open file";
        throw new FileException('file does not exist');
    } catch (Exception $e) {
        print $e -> fileInfo();
    }

    捕获多个异常

    class FileException extends Exception{}
    
    //文件不存在的异常
    class FileNotExistException extends FileException{}
    //文件权限异常 
    class FilePermissionException extends FileException{}
    
    function fileHandle(){
        //文件不存在
        throw new FileNotExistException('file does not exist');
    
        //文件权限异常
        throw new FilePermissionException('access denied');
    
        //其他异常
        throw new FileException('other exception');
    }
    try {
        fileHandle();
    } catch (FileNotExistException $e) {
        echo $e -> getMessage();
    }catch(FilePermissionException $e){
        echo $e -> getMessage();
    }catch(FileException $e){
        echo $e -> getMessage();
    }

    全局异常处理

    <?php 
    class FileException extends Exception{
        //文件不存在
        const FILE_NOT_EXIST = 1;
        //权限不够
        const FILE_PERMISSION = 2;
    }
    
    function fileHandle(){
        //文件不存在
        throw new FileException('FilenotExist',FileException::FILE_NOT_EXIST);
    
        //权限不够
        throw new FileException('FilePermission',FileException::FILE_PERMISSION);
    }
    try {
        fileHandle();
    } catch (Exception $e) {
        switch ($e -> getCode()) {
            case FileException::FILE_NOT_EXIST:
                echo($e->getMessage());
                break;
            case FileException::FILE_PERMISSION:
                echo ($e -> getMessage());
                break;
        }
    }catch(Exception $e){
        echo 'exception';
    }
    ?>
    ------------------------------------------
    <?php 
    function defaultExceptionHandle($e){
        printf("uncaught exception:%s in file %s on line %d",$e->getMessage(),$e->getFile(),$e->getLine());
    }
    
    set_exception_handler('defaultExceptionHandle');
    throw new Exception('tese......');
    ?>
  • 相关阅读:
    在yii中使用Filter实现RBAC权限自动判断
    关于WEB设计透明和阴影
    一句话扯扯数据结构的概念点
    Console API Google 浏览器开发人员工具使用
    git提交项目时候,忽略一些文件
    学习笔记 如何解决IE6 position:fixed固定定位问题{转载}
    [转载]yii jquery折叠、弹对话框、拖拽、滑动条、ol和ul列表、局部内容切换
    Jquery 常用方法经典总结【砖】
    PHP中冒号、endif、endwhile、endfor这些都是什么
    [转载]救命的PHP代码
  • 原文地址:https://www.cnblogs.com/chenyingying0/p/12191532.html
Copyright © 2011-2022 走看看