Catch not working and how to unset the exception handler

时间秒杀一切 提交于 2019-12-10 18:09:07

问题


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 exception 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


回答1:


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.




回答2:


The function is restore_exception_handler. However, the handler should only be called when an exception is unhandled. It does not disable catches.



来源:https://stackoverflow.com/questions/2714342/catch-not-working-and-how-to-unset-the-exception-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!