的通知也不例外,因此它不能在Try {}捕捉{}块被捕获。与警告类似。
一般来说一个更好的主意,有让他们显示那些在你的开发环境的error_reporting级别,你不得不处理这些问题,你的代码的方式。
然而,捕捉它们,对付它们的另一种方式,你要设置自定义错误处理程序。您可以让您的自定义错误处理程序仅捕获某些错误。
<?php
// set custom handler for NOTICES
// $oldHandler will allow you to reset the handler back to PHP default later
$oldHandler = set_error_handler('myErrorHandler', E_NOTICE|E_WARNING);
function myErrorHandler($errno, $errstr, $errfile, $errline) {
// log to DB in here
// $db->query("INSERT INTO error_log (errno, errstr...) VALUES (...)");
// return false to let the error bubble up and get caught by default error handler
// return false;
// return true to tell PHP you've dealt with it
return true;
}
// YOUR SAMPLe CODE SHOULD NOW CAPTURE THE NOTICE
$error = "";
try
{
echo $a; // not declared
}
catch(\Error $e)
{
$error = $e->getMessage();
}
?>
当你打印'$ error'时,你会得到什么? – Akintunde007
我认为这可以帮助:输出所有的PHP错误数据库不error_log中(http://stackoverflow.com/questions/2911094/outputting-all-php-errors-to-database-not-error-log) –
做你使用['set_error_handler()'](https://www.w3schools.com/php/func_error_set_error_handler.asp)? –