zoukankan      html  css  js  c++  java
  • php的错误处理机制

    看tp5源码的,补充下

    error_reporting(E_ALL);
    set_error_handler([__CLASS__, 'appError']);
    set_exception_handler([__CLASS__, 'appException']);
    register_shutdown_function([__CLASS__, 'appShutdown']);

    第一个是报告所有错误,就一个配置

    第二:set_error_handler

    function myErrorHandler($errno, $errstr, $errfile, $errline) {
        echo "<b>Custom error:</b> [$errno] $errstr<br>";
        echo " Error on line $errline in $errfile<br>";
    }
    
    // 设置用户定义的错误处理函数
    set_error_handler("myErrorHandler");
    
    $test=2;
    
    // 触发错误
    if ($test>1) {
        trigger_error("A custobeen triggered");
    }

    第三:_exception_handler

    function _exception_handler($e)
    {
        if ($e instanceof Error)
        {
            echo "catch Error: " . $e->getCode() . '   ' . $e->getMessage() . '<br>';
        }
        else
        {
            echo "catch Exception: " . $e->getCode() . '   ' . $e->getMessage() . '<br>';
        }
    }
    
    set_exception_handler('_exception_handler');    // 注册异常处理方法来捕获异常
    
    throw new Exception('This is a exception', 400);  //抛出一个Exception,看是否可以被catch*/

    第四:php挂掉之前做下日志的功能

    register_shutdown_function('shutdown_function');
    try
    {
        $a = new A();//这里会报致命错误
        echo 5/0;
    }
    catch(Exception $e)
    {
        echo '异常捕获';
        print $e->getMessage();
    }
    
    function shutdown_function()
    {
        echo '捕获错误';
        $e = error_get_last();
        print_r($e);
    }
    

      

    这是php的错误处理函数的基本解释和触发方法。tp5改写了下,值得看看。

  • 相关阅读:
    HDU 3068 Manacher
    HDU 6188最小费用流
    Codeforces Round #442 (Div. 2) Danil and a Part-time Job
    并查集
    HDU 5988最小网络流(浮点数)
    HOJ
    HOJ
    POJ
    POJ
    关于async
  • 原文地址:https://www.cnblogs.com/norm/p/9114993.html
Copyright © 2011-2022 走看看