导航首页 » 技术教程 » PHP异常处理:set_exception_handler()函数的用法
PHP异常处理:set_exception_handler()函数的用法 138 2023-12-14   

set_exception_handler

(PHP 5)

set_exception_handler — 设置一个用户定义的异常处理函数。

说明

callable set_exception_handler ( callable $exception_handler )

设置默认的异常处理程序,用于没有用 try/catch 块来捕获的异常。 在 exception_handler 调用后异常会中止。

参数

exception_handler

当一个未捕获的异常发生时所调用函数的名称。 该函数必须在调用 set_exception_handler() 之前已经定义。 该处理函数需要接受一个参数,该参数是一个抛出的异常对象。

Note:

也可以传递一个 NULL 值用于重置异常处理函数为默认值。

返回值

返回之前定义的异常处理程序的名称,或者在错误时返回 NULL。 如果之前没有定义一个错误处理程序,也会返回 NULL。 如果参数使用了 NULL,重置处理程序为默认状态,并且会返回一个 TRUE

范例

Example #1 set_exception_handler() 范例

<?php
function exception_handler($exception) {
  echo "Uncaught exception: " , $exception->getMessage(), "n";
}

set_exception_handler('exception_handler');

throw new Exception('Uncaught Exception');
echo "Not Executedn";
?>

参见

restore_exception_handler() - 恢复之前定义过的异常处理函数。 restore_error_handler() - 还原之前的错误处理函数 error_reporting() - 设置应该报告何种 PHP 错误 callback 类型的信息 PHP 5 Exceptions


!!!站长长期在线接!!!

网站、小程序:定制开发/二次开发/仿制开发等

各种疑难杂症解决/定制接口/定制采集等

站长微信:lxwl520520

站长QQ:1737366103