catch 不起作用,因为使用 set_exception_handler() 安装了异常处理程序
我需要“捕获”才能工作,所以我想我需要以某种方式取消设置异常处理程序。诸如 set_exception_handler(NULL) 之类的东西不起作用。
任何想法如何取消设置异常处理程序?
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());
}
实际输出:
捕获异常: Hello World
期望的输出:
应该发生: Hello World
最佳答案
restore_exception_handler
,它从 set_exception_handler
的手册条目链接。
顺便说一句,这些异常处理程序应该只在未捕获异常时发挥作用。 catch
块应始终具有优先级。
阅读 Exceptions 页面上的评论,您将了解 this bug 和 this bug 。它们准确地描述了您的体验,定义自定义错误处理程序时无法捕获异常。
解决方案:
关于php - Catch 不工作以及如何取消设置异常处理程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2714342/