Catch not working and how to unset the exception handler
catch is not working because there is installed an exception handler using set_exception_handler()
I need "catch" to work, so I guess I need to unset the ex开发者_Python百科ception handler somehow. Things such as set_exception_handler(NULL) isn't working.
Any ideas how to unset the exception handler?
function my_exception_handler($exception) {
error_log("caught exception: " . $exception->getMessage() );
}
set_exception_handler("my_exception_handler");
// QUESTION: how does on unset it ?
//set_exception_handler(NULL);
try {
throw new Exception('hello world');
error_log("should not happen");
} catch(Exception $e) {
error_log("should happen: " . $e->getMessage());
}
Actual output:
caught exception: hello world
Desired output:
should happen: hello world
restore_exception_handler
, which is linked from the manual entry for set_exception_handler
.
BTW, these exception handlers should only come into play when an exception is uncaught. A catch
block should always have precedence.
Reading a little bit in the comments on the Exceptions page brings you to this bug and this bug. They describe exactly what you experience, Exceptions can't be caught when a custom error handler is defined.
Solution:
Fixed in 5.3 and HEAD, won't be backported to 5.2.
The function is restore_exception_handler. However, the handler should only be called when an exception is unhandled. It does not disable catches.
精彩评论