2012-06-19 77 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()處理。