我正在用 PHP5 编写脚本,需要某些文件的代码。当 A 文件不可用于包含时,首先会抛出警告,然后抛出 fatal error 。当无法包含代码时,我想打印自己的错误消息。如果 requeire 不起作用,是否可以执行最后一个命令?以下无效:
require('fileERROR.php5') or die("Unable to load configuration file.");
使用 error_reporting(0)
抑制所有错误消息只会出现白屏,不使用 error_reporting 会出现我不想显示的 PHP 错误。
最佳答案
您可以使用 set_error_handler
来完成此操作连同ErrorException
.
ErrorException
页面的例子是:
<?php
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
set_error_handler("exception_error_handler");
/* Trigger exception */
strpos();
?>
一旦您将错误作为异常处理,您可以执行以下操作:
<?php
try {
include 'fileERROR.php5';
} catch (ErrorException $ex) {
echo "Unable to load configuration file.";
// you can exit or die here if you prefer - also you can log your error,
// or any other steps you wish to take
}
?>
关于php - 如何在 PHP 中捕获 require() 或 include() 的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8261756/