php捕获parse error 失败?
漂亮男人
漂亮男人 2017-05-16 13:00:20
0
3
403

代码如下,已经注册了错误处理函数register_shutdown_function都没有执行,很奇怪


    error_reporting(-1);
    ini_set('display_errors', 1);

    set_error_handler(function(){
        echo "error handler execute";
    }, E_ALL);

    set_exception_handler(function(){
        echo "exception handler execute";
    });

    register_shutdown_function(function(){
        echo "shutdown function execute";
    });

    try{
        0$a;
    }catch(exception $e){
        echo "catch exception";
    }finally{
        echo "finally ";
    }

执行结果:


Parse error: syntax error, unexpected '$a' (T_VARIABLE) in C:\Users\mao\Documents\php\index.php on line 18

PHP Parse error:  syntax error, unexpected '$a' (T_VARIABLE) in C:\Users\mao\Documents\php\index.php on line 18
[Finished in 0.1s]

0$a是故意写的,为什么异常都没有被处理呢?

漂亮男人
漂亮男人

reply all(3)
洪涛

You can try PHP7’s try{}catch(Error){}

http://php.net/manual/en/clas...

ParseError extends Error 
PHPzhong

Syntax errors are the first to be warned by the system and are system-level exceptions. As soon as the system warns, the entire program has never been run.

迷茫

First of all, you must understand that exceptions and errors are different. Exceptions are situations outside of normal logic, while errors refer to errors during runtime! Once an error occurs, the entire code will no longer be executed and your program will hang. If an exception occurs, you can use try catch to catch it, and the program can continue to run!

Obviously, your code has a syntax error, so this program cannot be executed at all, which means that you triggered an error instead of an exception. So how to achieve the effect you want? First we need to solve the syntax problem, look at the code below

    error_reporting(-1);
    ini_set('display_errors', 1);

    set_error_handler(function(){
        echo "error handler execute";
    }, E_ALL);

    set_exception_handler(function(){
        echo "exception handler execute";
    });

    register_shutdown_function(function(){
        echo "shutdown function execute";
    });

    try{
        echo $a;
    }catch(exception $e){
        echo "catch exception";
    }
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!