1 回答
TA贡献1906条经验 获得超3个赞
使用 set_exception_handler() 尝试抛出一个不存在的对象将导致 PHP 致命错误。
更多细节 -
1- https://www.php.net/manual/en/language.exceptions.php#language.exceptions.catch
2- https://www.php.net/manual/en/class.errorexception.php
尝试下面的代码,现在错误将被捕获。
function exception_error_handler($severity, $message, $file, $line) {
if (!(error_reporting() & $severity)) {
// This error code is not included in error_reporting
return;
}
if($message == 'Division by zero'){
throw new DivisionByZeroError('Division By Zero Error');
}else{
throw new ErrorException($message, 0, $severity, $file, $line);
}
}
set_error_handler("exception_error_handler");
function check ($func) {
try {
call_user_func($func);
}
catch (DivisionByZeroError $e) {
echo "An Division error occurred - ".$e->getMessage(); //$e->getMessage() will deisplay the error message
}
catch (Exception $e) {
echo "An error occurred - ".$e->getMessage(); //$e->getMessage() will deisplay the error message
}
}
function test () {
echo 4/0;
}
check("test");
- 1 回答
- 0 关注
- 171 浏览
添加回答
举报