2012-06-19 71 views
0

我有这样的代码,如何可以设置error_handler所有功能。现在只有当错误发生在foo1 & foo2之外时,才会调用error_handler。全局设置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); 
} 
+0

这两个'出口(0);'就没有任何意义。该代码从未执行。另外你的'error_handler'函数的参数不是真的正确。 – hakre

回答

0

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

您在使用中的功能是Exception,那么应该是set_exception_handler

0

error_handler仅针对由PHP本身或使用trigger_error触发的错误进行调用。

但是你的foo1()foo2()函数抛出异常;那些将不得不由set_exception_handler()处理。