Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php异常未捕获_Php_Exception Handling - Fatal编程技术网

php异常未捕获

php异常未捕获,php,exception-handling,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:\

我试图捕获异常,但无法捕获,只是收到了错误消息

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 
这不是一个例外(而是一个警告),所以你不能简单地抓住它

您可以取消显示警告(在本场景中不推荐),或者使用一些东西来验证

比如

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
}

签出并触发/抛出您自己的错误/异常。如果您已经要抛出错误,是否需要try/catch的副本?@Script47:您是对的,您不需要它,除非您希望以不同的方式处理异常,而不是直接进入if语句