dongshou9878 2018-07-08 20:46
浏览 46

捕获所有错误和异常

I know this is an old question before post this new question I checked relevant answers : answer my requirements :

I want php report any error I want my code to be able to catch and handle all errors and exceptions

in the snippet I set up handlers for any error and exception plus for shutdown I run it with PHP Version 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 />
";
            echo "  Fatal error on line $errline in file $errfile";
            echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />
";
            echo "Aborting...<br />
";
            exit(1);
            break;

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

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

        default:
            echo "Unknown error type: [$errno] $errstr<br />
";
            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(), "
";
    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');

?>

question uncatched exception

adding following lines in script code

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

I expected the exception was handled in class in try/catch block

but I get this output

error handler imvoked Unknown error type: [2] Division by zero completed myShutDownHandler invoked normal shutdown without errors

question parse error not catched

adding following lines in script code

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

I got standard php error msg => Parse error: syntax error, unexpected end of file in C:\wamp\www\apocalisse\public\snippet1.php on line 112

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用