捕获所有错误和异常

时间:2018-07-08 20:46:29

标签: php error-handling

在发布此新问题之前,我知道这是一个老问题 我检查了相关答案: answer 我的要求:

我希望php报告任何错误 我希望我的代码能够捕获并处理所有错误和异常

在代码段中,我为所有错误和异常以及关闭程序设置了处理程序 我使用PHP 7.0.29版运行它

    <?php

class A{
    public function __construct()
    {

    }
}


class B{
    public function __construct()
    {
    }


    public function ExceptionCatched()
    {
        try
        {
            $x=1;
            $y=0;
            $z= $x/$y;
        }
        catch (Exception $any)
        {
            print  "Exception catched in class B msg=".$any->getMessage()."<br>";
            die("completed");
        }
    }
}


/* this function should be invoked when the script completed
or whe error occurs
*/
function myShutDownHandler() {
    print "myShutDownHandler invoked<br>";
    $errfile = "unknown file";
    $errstr  = "shutdown";
    $errno   = E_CORE_ERROR;
    $errline = 0;

    $error = error_get_last();

    if( $error !== NULL) {
        //invoked by error
        $errno   = $error["type"];
        $errfile = $error["file"];
        $errline = $error["line"];
        $errstr  = $error["message"];
        $s="***shutdown  with error<br>";
        $s.="type:$errno<br>";
        $s.="file:$errfile<br>";
        $s.="line:$errline<br>";
        $s.="str:$errstr<br>";
            }
    else {
        //imvoked before script terminated
        $s = "normal shutdown without errors";
    }
    print $s;
    die("<hr>");
}

/*this function would be invoked for any error */
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
    print "error handler imvoked<br>";
    switch ($errno) {
        case E_USER_ERROR:
            echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
            echo "  Fatal error on line $errline in file $errfile";
            echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
            echo "Aborting...<br />\n";
            exit(1);
            break;

        case E_USER_WARNING:
            echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
            break;

        case E_USER_NOTICE:
            echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
            break;

        default:
            echo "Unknown error type: [$errno] $errstr<br />\n";
            break;
    }
    die("completed<br>");

}

/* this function should be invoked on every unhandle exception*/
function myExceptionHandler($exception) {
    print "myExceptionHandler invoked<br>";
    echo "Uncaught exception: " , $exception->getMessage(), "\n";
    print "<hr>";
}

//script code
ini_set('display_errors',0);
error_reporting(-1);
register_shutdown_function( "myShutDownHandler" );
$old_error_handler = set_error_handler("myErrorHandler");
set_exception_handler('myExceptionHandler');

?>

问题未捕获的异常

在脚本代码中添加以下行

$a=new  A();
$b=new B();
$b->ExceptionCatched();

我希望该异常是在try / catch块中的类中处理的

但是我得到了这个输出

调用了错误处理程序 未知错误类型:[2]除以零 已完成 myShutDownHandler被调用 正常关机,没有错误

未捕获问题解析错误

在脚本代码中添加以下行

$a=new  A();
$b=new B();
$b->ExceptionCatched()//semicolon omitted to force error

我收到标准的php错误消息msg =>解析错误:语法错误,第112行的C:\ wamp \ www \ apocalisse \ public \ snippet1.php中的文件意外结束

0 个答案:

没有答案