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 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口