【问题标题】:Why does set_exception_handler(func) not catch exceptions?为什么 set_exception_handler(func) 不捕获异常?
【发布时间】:2013-07-19 01:26:20
【问题描述】:

在我的关闭函数中抛出了一个异常,并且没有在 try/catch 块中被捕获,例如:

<?php

set_exception_handler(function($e){
    echo "exception handled"; // not echoed
});

register_shutdown_function(function(){
    throw new Exception("test"); // this should be caught by the exception handler above, but it doesn't
});

Live run.

运行上面的代码给出:

致命错误:未捕获的异常“异常”和消息“测试”

但是PHP Manual 声称:

set_exception_handler 设置默认异常处理程序如果在 try/catch 块中没有捕获到异常。 执行将在 exception_handler 后停止调用。

为什么exception_handler 没有捕捉到抛出的异常?

【问题讨论】:

    标签: php exception exception-handling shutdown php-internals


    【解决方案1】:

    因为超出范围...

    http://www.php.net/manual/en/function.register-shutdown-function.php

    在脚本执行完成或调用 exit() 后注册要执行的回调。

    当你的脚本执行完成并且实际上正在关闭时,它必须剥离你的处理程序。

    【讨论】:

    猜你喜欢
    • 2021-09-27
    • 2012-08-27
    • 2020-07-31
    • 1970-01-01
    • 2016-05-10
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多