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

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

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像