dongzhenge2014 2019-01-02 11:50
浏览 264
已采纳

register_shutdown_function()和set_error_handler()可以捕获相同的错误吗?

If I have the following defined in the same script:

register_shutdown_function('handlerOne');
set_error_handler('handlerTwo');

Are there any error types that would trigger both handlers?

  • 写回答

1条回答 默认 最新

  • dongxia9620 2019-01-02 12:13
    关注

    The shutdown function will be executed when the script execution is finished whether there is an error, exception or not. It has nothing to do with errors or exceptions, errors or exceptions don't trigger it , and it does not catch them, it will be called anyway at the end of the script, so it is useful if you want to do some work even if an exception or fatal error happened because error handler function does not get executed if Fatal error or exception happened.

    The error handler function will be executed when error is triggered. This is quoted from the manual

    The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called.

    <?php
    
    function shutdownFunction(){
        echo "shutdownFunction is called 
    ";
    } 
    
    function errorHandlerFunction(){
        echo "errorHandlerFunction is called 
    ";
    } 
    register_shutdown_function('shutdownFunction');
    set_error_handler('errorHandlerFunction');
    
    //echo "foo
    "; // scenario 1 no errors
    //echo $undefinedVar; //scenario 2 error is triggered
    //undefinedFunction(); //scenario 3 Fatal error is triggered
    //throw new \Exception(); //scenario 4 exception is thrown
    

    scenario 1 (no errors) outputs

    foo 
    shutdownFunction is called
    

    scenario 2(error is triggered) outputs

    errorHandlerFunction is called 
    shutdownFunction is called 
    

    scenario 3 (Fatal error is triggered) outputs

    Fatal error: Call to undefined function undefinedFunction() in /tmp/execpad-b2a446c7f6a6/source-b2a446c7f6a6 on line 15
    shutdownFunction is called
    

    scenario 4 (exception is thrown) outputs

    Fatal error: Uncaught exception 'Exception' in /tmp/execpad-0b3a18f0ea06/source-0b3a18f0ea06:16
    Stack trace:
    #0 {main}
    thrown in /tmp/execpad-0b3a18f0ea06/source-0b3a18f0ea06 on line 16
    shutdownFunction is called 
    

    see for yourself https://eval.in/1073642

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下