PHP - set_exception_handler() 函式



語法

string set_exception_handler ( callback $exception_handler );

定義和用法

如果異常在 try/catch 塊中未被捕獲,此函式將設定預設的異常處理程式。在呼叫 exception_handler 後,執行將停止。

引數

序號 引數和描述
1

exception_handler

當發生未捕獲的異常時要呼叫的函式的名稱。在呼叫 set_exception_handler() 之前必須定義此函式。此處理程式函式需要接受一個引數,該引數將是丟擲的異常物件。

返回值

它返回先前定義的異常處理程式的名稱,或在出錯時返回 NULL。如果未定義先前的處理程式,則也返回 NULL。

示例

以下是此函式的用法:

<?php
   function exception_handler($exception) {
      echo "Uncaught exception is : " , $exception->getMessage(), "\n";
   }
   
   set_exception_handler('exception_handler');
   set_exception_handler();
   
   throw new Exception('Not Found Exception');
   echo "not included Executed\n";
?> 

這將產生以下結果:

Uncaught exception is: Not Found Exception
php_function_reference.htm
廣告