1
我建設我的錯誤報告系統的基礎知識,基本上我有這兩個壞男孩成立set_exception_handler()不捕捉異常
class System {
public function __construct(){
set_exception_handler(array($this, 'handleExceptions'));
set_error_handler(array($this, 'handleGenericError'));
}
public function handleExceptions(\Exception $e){
echo "<pre>".print_r($e, true)."</pre>";
}
public function handleGenericError($no, $msg, $file, $line, $context){
$msg .= ' in ['.$file.'] on line ['.$line.']';
throw new \Exception($msg);
}
}
然後,我挑起了__construct
方法錯誤,聲明之後。像
public function __construct(){
....
echo $undefined_variable;
}
而且
public function __construct(){
....
echo $this->undefined_variable;
}
聲明似乎進展順利,打印一個不錯的可讀性異常消息,但如果我這樣做
public function __construct(){
....
echo $this->$undefined_variable;
}
我得到一個未捕獲的異常。這是爲什麼?拋出的異常是從handleGenericError
方法,我可以告訴,因爲它有它周圍的東西[]
。這有點令人困惑。 :/
但它是一個如此大的程序邏輯失敗,它如何設法拋出異常? –
我相信這是一個邏輯缺陷,但只有在這種情況下處理異常的好方法。這意味着如果你沒有一個異常處理程序,錯誤會在最大範圍內產生一個關於未定義變量的消息。但在這個範圍內,它確實需要經過一個處理程序,但處理程序不知道如何處理它。邏輯上的缺陷循環暗示了 - 它說了一些事實 - 它可以處理它。但事實並非如此。不知道有沒有在PHP核心世界討論這個問題?值得在PHP核心開發領域進行搜索。 – JakeGould