PHP ErrorException
介绍
PHP的Exception类实现Throwable接口。ErrorException类扩展Exception类。当想要捕获和处理否则会被忽略的错误(例如,通知或警告)时,将明确抛出ErrorException。
PHP核心由以下预定义的错误常量组成
E_ERRORE_WARNINGE_PARSEE_NOTICEE_CORE_ERRORE_CORE_WARNINGE_COMPILE_ERRORE_COMPILE_WARNINGE_USER_ERRORE_USER_WARNINGE_USER_NOTICEE_STRICTE_RECOVERABLE_ERRORE_DEPRECATEDE_USER_DEPRECATEDE_ALL除了从Exception类继承的属性和方法外,ErrorException类还引入了一种属性和一种方法,如下所示:
protected int severity ; final public getSeverity ( void ) : int
上表中异常的严重程度由与错误类型相关的整数表示
ErrorException示例
在以下脚本中,使用set_error_handler()函数将用户定义的函数errhandler设置为Errorhandler。当遇到找不到要读取的文件的事件时发生致命错误时,它将引发ErrorException。
示例
<?php
function errhandler($severity, $message, $file, $line) {
if (!(error_reporting() & $severity)) {
echo "no error";
return;
}
throw new ErrorException("Fatal Error:No such file or directory", 0, E_ERROR);
}
set_error_handler("errhandler");
/* Trigger exception */
try{
$data=file_get_contents("nofile.php");
echo $data;
}
catch (ErrorException $e){
echo $e->getMessage();
}
?>上面的示例显示以下输出
输出结果
Fatal Error:No such file or directory