PHP的錯誤和異常處理總結

來源:互聯網
上載者:User

PHP的錯誤和異常處理總結

PHP內建了一批與錯誤和異常處理相關的 函數,本文會對其中部分函數進行詳細說明。

http://leo108.com

set_error_handler和 restore_error_handler

set_error_handler可以設定當程式出現錯誤時,將對應的錯誤交給使用者自訂的邏輯來處理。 leo108's blog

但是並不是所有的錯誤都可以被 set_error_handler所指定的處理邏輯捕獲,例如: E_ERROR、 E_PARSE、 E_CORE_ERROR、 E_CORE_WARNING、 E_COMPILE_ERROR、 E_COMPILE_WARNING,簡單來說就是原本就會導致程式終止的錯誤都無法被捕獲。 PHP的錯誤和異常處理總結

set_error_handler只能設定一個錯誤處理邏輯,多次調用 set_error_handler只有最後一次的那生效:

leo108's blog

set_error_handler(function($errno, $errstr){    echo 'catch error1';});set_error_handler(function($errno, $errstr){    echo 'catch error2';});trigger_error('something error');

輸出的是 leo108's blog

catch error2

http://leo108.com

在編寫一個第三方程式碼程式庫的時候,如果希望能夠捕獲程式碼程式庫中的錯誤,又不影響調用方對錯誤的處理,可以使用 restore_error_handler函數: PHP

function third_party_function() {//第三方類庫的錯誤處理邏輯set_error_handler(function() {//一些代碼});//該第三方類庫的邏輯//一些代碼//複原錯誤處理邏輯restore_error_handler();}

這樣,第三方程式碼程式庫中出現的錯誤就會被第三方程式碼程式庫的錯誤處理邏輯捕獲,而在這個第三方程式碼程式庫之外的錯誤還是由原本的邏輯處理。

異常

如果錯誤處理邏輯的傳回值是 false,則PHP會調用內建的錯誤處理邏輯(例如根據 error_reporting的值判斷是否列印錯誤資訊,寫錯誤記錄檔等);而返回 true則不會調用內建處理邏輯,在執行完自訂的錯誤處理邏輯之後,會返回觸發錯誤的地方繼續往下執行。

推酷是個無恥的網站

error_reporting以及 @符號不會影響錯誤處理邏輯的調用:

推酷是個無恥的網站

error_eporting(0);set_error_handler(function($errno, $errstr){    echo 'catch error';});@trigger_error('something error');

上述代碼會輸出:

PHP

catch error

leo108's blog

對於無法被 set_error_handler捕獲的錯誤常見的只有 E_ERROR和 E_PARSE兩個。前者可能是記憶體使用量超過設定的值、執行個體化一個不存在的類或者是調用一個不存在的函數;後者通常是文法解析錯誤例如漏寫一個分號。對於 E_PARSE,由於是文法層面的錯誤,所以沒有辦法去捕獲處理。但對於 E_ERROR則可以通過 register_shutdown_function以及 error_get_last兩個函數來處理,範例程式碼如下:

推酷是個無恥的網站

error_reporting(0);register_shutdown_function(function() { $error = error_get_last(); if ($error != null && $error['type'] == E_ERROR) {echo "fatal error catched:" . var_export($error, true); }});new test();

輸出:

http://leo108.com

fatal error catched:array (

http://leo108.com

‘type’ => 1, PHP的錯誤和異常處理總結

‘message’ => ‘Class ‘test’ not found’, PHP的錯誤和異常處理總結

‘file’ => ‘/tmp/error.php’,

推酷是個無恥的網站

‘line’ => 20,

PHP

)

leo108's blog

set_exception_handler和 restore_exception_handler

這兩個函數和 set_error_handler的兩個函數差不多,同樣是只能註冊一個異常處理邏輯,多次註冊只有最後一個生效;可以通過 restore_exception_handler來恢複之前的異常處理邏輯。 leo108's blog

一些小實驗

在錯誤處理邏輯中觸發錯誤

set_error_handler(function($errno, $errstr){    echo 'catch error:' . $errstr . PHP_EOL;    trigger_error('error in error handler');});trigger_error('origin error');

輸出: leo108's blog

catch error:origin error

異常

PHP Notice: error in error handler in /tmp/error.php on line 15

PHP的錯誤和異常處理總結

結論:在錯誤處理邏輯中的錯誤是無法被再次捕獲。 異常

在異常處理邏輯中拋出異常

set_exception_handler(function(Exception $e){    echo 'catch exception:' . $e->getMessage() . PHP_EOL;    throw new Exception('exception in exception handler');});throw new Exception('origin exception');

輸出: 推酷是個無恥的網站

catch exception:origin exception

推酷是個無恥的網站

PHP Fatal error: Uncaught exception ‘Exception’ with message ‘exception in exception handler’ in /tmp/error.php:15 http://leo108.com/pid-2216.asp

Stack trace: 異常

0 [internal function]: {closure}(Object(Exception))

1 [main]

thrown in /tmp/error.php on line 15 http://leo108.com

結論:在異常處理邏輯中拋出的異常不會被捕獲 http://leo108.com/pid-2216.asp

同時定義了異常和錯誤處理邏輯,在錯誤處理邏輯中拋出異常,在異常處理邏輯中觸發錯誤

set_exception_handler(function(Exception $e){    echo 'catch exception:' . $e->getMessage() . PHP_EOL;    trigger_error('error in exception handler');});set_error_handler(function($errno, $errstr){    echo 'catch error:' . $errstr . PHP_EOL;    throw new Exception('exception in error handler');});

外部觸發了錯誤

trigger_error('origin error');

輸出:

異常

catch error:origin error

http://leo108.com

catch exception:exception in error handler

http://leo108.com

catch error:error in exception handler

異常

PHP Fatal error: Uncaught exception ‘Exception’ with message ‘exception in error handler’ in /tmp/error.php:9

leo108's blog

Stack trace:

PHP

0 [internal function]: {closure}(1024, ‘error in except…’, ‘/tmp/error.php’, 5, Array)

1 /tmp/error.php(5): trigger error(‘error in except…’)

2 [internal function]: {closure}(Object(Exception))

3 [main]

thrown in /tmp/error.php on line 9

http://leo108.com/pid-2216.asp

結論:調用了兩次錯誤處理邏輯,一次異常處理邏輯。

推酷是個無恥的網站

外部拋出異常

throw new Exception('origin exception');

輸出:

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.