我不能做这样的事情吗?
try {
require_once( '/includes/functions.php' );
}
catch(Exception $e) {
echo "Message : " . $e->getMessage();
echo "Code : " . $e->getCode();
}
没有回显错误,服务器返回 500。
最佳答案
您可以使用 include_once
或 file_exists
:
try {
if (! @include_once( '/includes/functions.php' )) // @ - to suppress warnings,
// you can also use error_reporting function for the same purpose which may be a better option
throw new Exception ('functions.php does not exist');
// or
if (!file_exists('/includes/functions.php' ))
throw new Exception ('functions.php does not exist');
else
require_once('/includes/functions.php' );
}
catch(Exception $e) {
echo "Message : " . $e->getMessage();
echo "Code : " . $e->getCode();
}
关于php - Try Catch 不能在 PHP 中使用 require_once?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5369488/