PHP Conference Japan 2024

restore_exception_handler

(PHP 5, PHP 7, PHP 8)

restore_exception_handler 還原先前定義的例外處理函式

說明

restore_exception_handler(): true

在使用 set_exception_handler() 更改例外處理函式後,使用此函式可恢復先前的例外處理函式(可以是內建的或使用者定義的函式)。

參數

此函式沒有參數。

回傳值

永遠回傳 true

範例

範例 #1 restore_exception_handler() 範例

<?php
function exception_handler_1(Exception $e)
{
echo
'[' . __FUNCTION__ . '] ' . $e->getMessage();
}

function
exception_handler_2(Exception $e)
{
echo
'[' . __FUNCTION__ . '] ' . $e->getMessage();
}

set_exception_handler('exception_handler_1');
set_exception_handler('exception_handler_2');

restore_exception_handler();

throw new
Exception('This triggers the first exception handler...');
?>

上述範例將輸出

[exception_handler_1] This triggers the first exception handler...

參見

新增註記

使用者貢獻的註記 1 則註記

13
rl7 at shinyblue dot net
10 年前
請注意,這在例外處理函式內部無效。

例如:

<?php

函式 handler1(Exception $e) {
echo
"handler1\n";
restore_exception_handler();
throw
$e;
}
函式
handler2(Exception $e) {
echo
"handler2\n";
}

set_exception_handler( 'handler2' );
set_exception_handler( 'handler1' );

throw new
Exception( '可能會預期看到 handler1, handler2' );

/* 輸出:
handler1
PHP 致命錯誤:未捕獲的例外 'Exception',訊息為 '可能會預期看到 handler1, handler2',位於 /tmp/demo.php:15
堆疊追蹤:
#0 {main}
拋出於 /tmp/demo.php 的第 15 行
*/

?>
To Top