根据http://php.net/manual/en/language.errors.php7.php的理解,现在应该抛出PHP7中的错误。但是在我自己的测试中,情况似乎并非如此:
<?php
error_reporting(E_ALL);
try {
echo $a[4];
} catch (Throwable $e) {
echo "caught\n";
}
echo "all done!\n";
在那种情况下,我希望“caught”被回显,然后脚本说“一切完成!”。相反,我得到这个:
Notice: Undefined variable: a in C:\games\test-ssh3.php on line 12
all done!
我误会了吗?
最佳答案
仅针对某些以前会停止执行的错误类型(E_RECOVERABLE_ERROR
)引发异常。警告和注意不会停止执行,因此不会引发异常(为此找到了source)。
您必须定义一个custom error handler并在其中抛出异常。 PHP声明也不异常(exception),因此不会通过try/catch
块捕获。
set_error_handler('custom_error_handler');
function custom_error_handler($severity, $message, $filename, $lineno) {
throw new ErrorException($message, 0, $severity, $filename, $lineno);
}
try {
echo $a[4];
} catch (ErrorException $e) {
echo $e->getMessage().PHP_EOL;
}
echo "all done!\n";
关于php - 不会抛出PHP 7中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48663051/