Catch FatalErrorException PHP

时间:2016-11-15 08:41:44

标签: php laravel

大家好!在我的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()

所以,我不明白为什么这段代码不处理错误异常以及如何解决这个问题!

1 个答案:

答案 0 :(得分:2)

除非您已使用&#34导入了声明FatalErrorException的命名空间,否则请使用"你需要确定例外的范围,如下所示:

catch (\Symfony\Component\Debug\Exception\FatalErrorException $e) {

否则,您将使用您的类所在的任何命名空间并尝试捕获在该命名空间中声明的异常。看起来你的ErrorException类似地设置了。

我不确定上面的命名空间是你的课程派生的名称,我只是猜测是因为你正在使用Laravel。

相关问题