捕获FatalErrorException PHP

捕获FatalErrorException PHP,php,laravel,Php,Laravel,大家好在我的Laravel应用程序中,我有一个Excel文件上传功能。我从网上得到了这段代码,并根据我的应用程序进行了调整。问题是,它没有捕获致命错误异常,该异常是在用户提交文件但未选择文件时产生的。我不明白为什么它没有被抓住。我将添加控制器的一部分 public function upload() { $file = array('thefile' => Input::file('thefile')); $rules = array('excel' =&

大家好在我的Laravel应用程序中,我有一个Excel文件上传功能。我从网上得到了这段代码,并根据我的应用程序进行了调整。问题是,它没有捕获致命错误异常,该异常是在用户提交文件但未选择文件时产生的。我不明白为什么它没有被抓住。我将添加控制器的一部分

 public function upload() {
        $file = array('thefile' => Input::file('thefile'));
        $rules = array('excel' => 'excel');
        $validator = Validator::make($file, $rules);
        if ($validator->fails()) {
            return Redirect::to('UploadExcelFile')->withInput()->withErrors($validator);
        }
        else {
            try{
            // FatalErrorException happens in this line!
            if (Input::file('thefile')->isValid()) {
                $destinationPath = 'uploads';
                $fileName = Input::file('thefile')->getClientOriginalName();
                Input::file('thefile')->move($destinationPath, $fileName);
                Session::flash('success', 'Upload successfully');
                $fileNameJSON = exec("python /path/to/script/ExcelToJSON3.py $fileName"); // This returns a full path name of the JSON file that is made...
                if ($fileNameJSON == null){
                    return Redirect::to('/dashboard/input');
                }
                else {

                //      Getting the ID from the file that is uploaded
                try {
                    $jsonDecode = json_decode(file_get_contents($fileNameJSON));
                } catch (ErrorException $e){
                    return Redirect::to('/errorpage')->with(array('status'=> 'ErrorException'));
                }


A lot of code for handling the data entered.... 

                }catch (FatalErrorException $e){
                        return Redirect::to('/errorpage')->with(array('status'=> 'FatalErrorException'));

            }    
        }
        return true;
    }
给出的错误是:

UploadExcelFileController.php第35行中的FatalErrorException: 对非对象调用成员函数isValid()


所以,我不明白为什么这段代码不处理错误异常,以及如何修复它

除非您已导入使用“use”声明FatalErrorException的命名空间,否则您将需要限定异常的范围,如下所示:

catch (\Symfony\Component\Debug\Exception\FatalErrorException $e) {
否则,您将使用类所在的任何名称空间,并尝试捕获在该名称空间中声明的异常。看起来您的ErrorException设置类似


我不确定上面的名称空间是否是您的类派生的名称空间,我只是猜测这是因为您使用的是Laravel。

是否将类顶部的
Symfony\Component\Debug\Exception\FatalErrorException
导入为
使用Symfony\Component\Debug\Exception\FatalErrorException
?是的!我已经导入了带有完整路径名的FatalErrorException,没有完整路径名,就像@Andy建议的那样。但我还是犯了同样的错误..嗨!我已将顶部的名称空间作为“use FatalErrorException”导入,我已尝试了您的建议,但仍然出现错误。。你认为这可能是因为顺序的关系吗?你使用的是哪个版本的PHP?对非对象调用方法实际上是一个引擎级致命错误,我怀疑发生的情况是处理这些错误的函数捕获了FatalErrorException。只有PHP7会将致命异常作为异常抛出。PHP5.5.9-1ubuntu4.19(cli)是的,这实际上是一个致命错误引擎错误,而不是在非对象上调用方法时发生的异常。我认为您看到的异常是在使用register\u shutdown\u function()设置后,在shutdown运行的自定义错误处理类中抛出的。在我看来,最好的办法是在调用对象上的方法之前,首先检查正在使用的对象是否是对象。您可以尝试使用is_object(),甚至可以确定它的类。在PHP7中,您将能够捕获(错误$e)以捕获该特定错误。感谢您的解释!