为什么set_exception_handler(func)没有捕获异常?

时间:2013-07-19 16:54:38

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

我的关闭函数抛出异常,并且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.

运行上面的代码给出:

  

致命错误:未捕获的异常'异常',消息'test'

然而PHP Manual声称:

  

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

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

1 个答案:

答案 0 :(得分:0)

因为它超出了范围......

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

  

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

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

相关问题