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 關注
- 202 瀏覽
添加回答
舉報