0

我有这段代码,如何将 error_handler 设置为所有函数。现在只有在 foo1 和 foo2 之外发生错误时才会调用 error_handler。

set_error_handler('error_handler',-1 & ~E_NOTICE & ~E_USER_NOTICE);

function error_handler($exception) {
// log the error
}


function foo1(){
throw new Exception("Error validating user input.");
        exit(0);
}


function foo2(){
throw new Exception("Error validating user input.");
        exit(0);
}
4

2 回答 2

0

set_error_handler设置了全局错误处理程序,它已经用于所有功能。对于 using set_error_handler,您应该使用trigger_error来触发错误,而不是抛出异常,未捕获的异常只会导致致命错误。

Exception在你的函数中使用,那么它应该是 set_exception_handler

于 2012-06-19T04:35:55.063 回答
0

error_handler唯一会调用由 PHP 本身或使用trigger_error.

但是您的foo1()andfoo2()函数正在抛出异常;那些必须由set_exception_handler().

于 2012-06-19T04:36:05.647 回答