4

在我的关闭函数中引发了异常,并且在 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 手册声称:

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

为什么不exception_handler捕获抛出的异常?

4

1 回答 1

0

因为超出范围...

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

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

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

于 2014-03-28T19:54:47.013 回答