php异常没有捕获

时间:2016-01-21 15:43:25

标签: php exception-handling

我正在尝试捕获异常,但无法,只是收到错误消息

try{
    echo 'test';
    require_once "jj.php";
    return true;
} catch (Exception $ex) {
    throw new Exception("error occured");
}



 Warning: require_once(jj.php): failed to open stream: No such file or directory in C:\xampp\htdocs\corporate-wellness\module\Survey\src\Survey\Repository\QuestionRepository.php on line 

1 个答案:

答案 0 :(得分:5)

这不是例外(但是警告),所以你不能简单地抓住它。

您可以改为禁止警告(在此方案中不推荐)或使用某些内容来验证file exists

所以,像

try {
    if(!file_exists("jj.php")) {
        throw new Exception("File doesn't exists");
    }
    require_once "jj.php";
    return true;
} catch (Exception $ex) {
    // if you would you can handle exception here or you can simply
    // throw exception without try - catch block
}