get_exception_handler
(PHP 8 >= 8.5.0)
get_exception_handler — 获取用户定义的异常处理函数
参数
此函数没有参数。
示例
示例 #1 get_exception_handler() 示例
<?php
$handler = function (Throwable $ex) {
echo "Exception: " . $ex::class . ": " . $ex->getMessage() . "\n";
};
var_dump(get_exception_handler()); // NULL
set_exception_handler($handler);
var_dump(get_exception_handler() === $handler); // bool(true)
?>注释
小技巧
在 PHP 8.5.0 之前,以下 polyfill 可以提供此功能:
<?php
if (!function_exists('get_exception_handler')) {
function noop_exception_handler() {
}
function get_exception_handler(): ?callable {
$handler = set_exception_handler('noop_exception_handler');
restore_exception_handler();
return $handler;
}
}
?>参见
- set_exception_handler() - 设置用户自定义的异常处理函数
- restore_exception_handler() - 恢复之前定义过的异常处理函数。
- restore_error_handler() - 还原之前的错误处理函数
- error_reporting() - 设置应该报告何种 PHP 错误
- 异常