問(wèn)題描述
我正在用 PHP5 編寫(xiě)一個(gè)需要某些文件代碼的腳本.當(dāng) A 文件不可用于包含時(shí),首先會(huì)引發(fā)警告,然后會(huì)引發(fā)致命錯(cuò)誤.當(dāng)無(wú)法包含代碼時(shí),我想打印自己的錯(cuò)誤消息.如果 requeire 不起作用,是否可以執(zhí)行最后一個(gè)命令?以下方法無(wú)效:
I'm writing a script in PHP5 that requires the code of certain files. When A file is not available for inclusion, first a warning and then a fatal error are thrown. I'd like to print an own error message, when it was not possible to include the code. Is it possible to execute one last command, if requeire did not work? the following did not work:
require('fileERROR.php5') or die("Unable to load configuration file.");
使用 error_reporting(0)
抑制所有錯(cuò)誤消息只會(huì)產(chǎn)生白屏,不使用 error_reporting 會(huì)產(chǎn)生 PHP 錯(cuò)誤,我不想顯示.
Supressing all error messages using error_reporting(0)
only gives a white screen, not using error_reporting gives the PHP-Errors, which I don't want to show.
推薦答案
您可以使用 set_error_handler
與 ErrorException
.
You can accomplish this by using set_error_handler
in conjunction with ErrorException
.
ErrorException
頁(yè)面中的示例是:
<?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();
?>
一旦您將錯(cuò)誤作為異常處理,您可以執(zhí)行以下操作:
Once you have errors being handled as exceptions you can do something like:
<?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
}
?>
這篇關(guān)于如何在 PHP 中捕獲 require() 或 include() 的錯(cuò)誤?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!