我编写了错误处理类,它将所有错误分为正常错误(通知、警告……)和严重错误。
现在我发现将所有错误转换为异常是一个很好的做法。它还会缩短我的代码。
但是,我不知道如何处理这个问题......
- 是否存在不会停止脚本执行的异常以及会停止脚本执行的异常?如果没有...如何区分转换后的错误?
如果捕获到异常,则不会停止脚本执行。要识别转换后的错误:
try {
// ...
} catch (ErrorException $e) {
// converted error (probably)
} catch (Exception $e) {
// another kind of exception; this basically catches all
}
Run Code Online (Sandbox Code Playgroud)
或者:
function handle_exception(Exception $e)
{
if ($e instanceof ErrorException) {
// converted error (probably)
} else {
// another kind of exception
}
}
set_exception_handler('handle_exception');
Run Code Online (Sandbox Code Playgroud)
请注意,ErrorException任何代码都可以抛出 ,但它set_error_handler()仅用于转换注册函数中的常规错误。
- 将错误转换为异常是通过调用 set_error_handler() 并在那里抛出新的 ErrorException() 来完成的......下一步是什么?set_exception_handler() 是自动调用的吗?
如果ErrorException错误处理函数抛出的异常没有在代码中的其他任何地方捕获,set_exception_handler()则将调用已注册的异常处理程序(使用 设置)。
| 归档时间: |
|
| 查看次数: |
4966 次 |
| 最近记录: |