【发布时间】:2014-08-21 20:58:12
【问题描述】:
如何捕获我在自定义关闭函数中抛出的(自定义)异常(使用自定义异常处理程序)?我没有使用任何框架。
例子:
register_shutdown_function( 'shutdownFunction');
set_exception_handler( 'exceptionHandler');
function exceptionHandler(Exception $exception)
{
// handle and log exception for later debugging...
}
function shutdownFunction()
{
// something is not going right...
if (someVariable != someValue)
throw new RuntimeException('blah...'); // WILL NOT be caught by exception handler
}
// somewhere else in the code...
throw new Exception('something...'); // WILL be caught by exception handler (but not in shutdown function)
脚本正在使用异常来传达它在执行期间遇到的错误,即。意外变量传递给函数,数据库无法插入行等...
【问题讨论】:
-
任何未捕获的异常现在都将转到
exceptionHandler并结束终止 PHP 应用程序。您无法从未捕获的异常中恢复。您可以使用try...catch块捕获错误