PHP set_exception_handler() 関数

PHP エラー・リファレンス

ユーザ定義の例外ハンドラ関数を設定します:

<?php
// A user-defined exception handler function
function myException($exception) {
    echo "<b>Exception:</b> ", $exception->getMessage();
}

// Set user-defined exception handler function
set_exception_handler("myException");

// Throw exception
throw new Exception("Uncaught exception occurred!");
?>

上のコードの出力は、次の通り:

Exception: Uncaught exception occurred!


定義と用法

set_exception_handler() 関数は、ユーザ定義の例外ハンドラ関数を設定します。

スクリプトの実行は、例外ハンドラがコールされた後に 停止します。


構文

set_exception_handler(exceptionhandler);

パラメータ 説明
exceptionhandler 必須。キャッチされない例外が発生した時に実行する関数名を指定する。 代わりに NULL を渡して、このハンドラをデフォルト状態にリセットすることができる

技術内容
返り値: 前に定義された例外ハンドラの名前、またはエラー発生時に NULL を返します。 前にハンドラが定義されていない場合にも NULL が返されます。
PHP バージョン: 5.0+
PHP 変更歴 これより前のバージョンでは、NULL を渡した場合の返り値が TRUE でした。 PHP 5.5.0 以降は、以前に設定されていたハンドラを返すようになりました

PHP エラー・リファレンス