我使用以下函數來設置我自己的錯誤處理程序和異常處理程序。錯誤處理程序中拋出的PHP異常不會被異常處理程序捕獲
set_error_handler
set_exception_handler
錯誤處理程序將錯誤轉換爲異常。 (拋出一個新的異常)
但是這些異常不會被我自己的異常處理程序捕獲。
錯誤處理程序例如:
function errorHandler($errno, $errstr, $errfile, $errline, $errcontext) {
throw new Exception("this was an error");
}
異常處理程序例如:
function exceptionHandler($e){
// don't get here when exception is thrown in error handler
Logger::logException($e);
}
(我認爲這不能無論如何工作)
如若這項工作?
或者有人可以解釋爲什麼它不能工作?
編輯:
我做了一些測試,它應該工作。在拋出的ErrorHandler
例外越來越被的ExceptionHandler 並在觸發的ExceptionHandler陷入錯誤越來越被的ErrorHandler
僅供參考處理。
我的問題在其他地方
編輯:
我還沒有發現,爲什麼在我的ErrorHandler拋出的異常沒有被我抓到exceptionHandler的。
例如,當我在代碼中有這個地方。
trigger_error("this is an error"); // gets handled by the errorHandler
throw new Exception("this is an exception"); // gets handler by the exceptionHandler
錯誤被errorHandler處理,但errorHandler中引發的異常不會被exceptionHandler處理。
但是,如果我在觸發錯誤的同一位置拋出異常,則此異常將由異常處理程序處理。
(希望這是某種可以理解我的意思)
我無言以對這裏。 任何想法,我必須尋找問題?
儘管我不再在解決這個問題,但您提供了正確的答案並不錯。不知何故,這是有道理的,但我沒有想到它。謝謝你的答案。 – superbly 2013-10-11 11:30:45
看起來像5.5中修復 - https://3v4l.org/K9NAR – 2015-11-14 16:34:46
,這是沒有意義的。爲什麼PHP應該決定退出E_ERROR,甚至沒有達到它? – emfi 2017-07-11 09:58:13