PHP 中的 set_exception_handler() 函数
phpprogrammingserver side programming更新于 2025/9/17 4:07:17
set_exception_handling() 函数用于设置用户自定义函数来处理异常。如果未在 try/catch 块中捕获异常,则设置默认异常处理程序。调用 exception_handler 后,程序将停止执行。
语法
set_exception_handling(exception_handler)
参数
exception_handler − 发生未捕获异常时调用的函数名称。此函数必须在调用 set_exception_handler() 之前定义。此处理函数需要接受一个参数,即抛出的异常对象。
返回
set_exception_hadler() 函数返回先前定义的异常处理程序的名称,如果发生错误则返回 NULL。如果先前未定义任何处理程序,则也返回 NULL。
示例
下面是一个例子 −
<?php
function exception_handler($exception) {
echo "Uncaught exception = " , $exception->getMessage(), "
";
}
set_exception_handler('exception_handler');
throw new Exception('Not Found Exception');
echo "not included Executed
";
?>
输出
以下是输出 −
Uncaught exception = Not Found Exception

