php - Try Catch 不能在 PHP 中使用 require_once?

标签 php

我不能做这样的事情吗?

try {
    require_once( '/includes/functions.php' );      
}
catch(Exception $e) {    
    echo "Message : " . $e->getMessage();
    echo "Code : " . $e->getCode();
}

没有回显错误,服务器返回 500。

最佳答案

您可以使用 include_oncefile_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/

相关文章:

PHP 正确的 if 语句?

php - 无法使用 Lapack : Class 'Lapack' not found in

php - 如何在PHP中使用curl GET发送原始数据?

PHP Manager IIS 无法安装 Windows 10

PHP 错误警告 : Parameter 1 expected to be a reference

php - 如何判断Web应用程序是否已经安装和设置?

php - 如何删除依赖表的行

javascript - 获取 URL 的基本名称,然后将事件类放入导航栏

php - 如何在 ipage 中从 godaddy 访问 mysql 数据库,因为 ipage 已阻止远程访问

php - Eloquent 查找错误的列