【问题标题】:Catch FatalErrorException PHP捕获 FatalErrorException PHP
【发布时间】:2016-11-15 08:41:44
【问题描述】:

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

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

【问题讨论】:

  • 您是否将类顶部的Symfony\Component\Debug\Exception\FatalErrorException 导入为use Symfony\Component\Debug\Exception\FatalErrorException
  • 是的!我已经导入了带有完整路径名的 FatalErrorException,没有完整路径名,就像@Andy 建议的那样。但我仍然得到同样的错误..

标签: php laravel


【解决方案1】:

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

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

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

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

【讨论】:

  • 嗨!我已将顶部的命名空间导入为“使用 FatalErrorException”,我尝试了您的建议,但仍然收到错误。您认为这可能是因为订单吗?
  • 您使用的是哪个版本的 PHP?在非对象上调用方法实际上是引擎级别的致命错误,我怀疑正在发生的事情是 FatalErrorException 被处理这些错误的函数捕获。只有 PHP7 会将致命异常作为异常抛出。
  • PHP 5.5.9-1ubuntu4.19 (cli)
  • 是的,它实际上是一个致命的错误引擎错误,而不是当您在非对象上调用方法时发生的异常。我认为您看到的异常是在使用 register_shutdown_function() 设置后在关机时运行的自定义错误处理类中引发的。在我看来,您最好的选择是在调用方法之前首先检查您正在使用的东西是一个对象。您可以尝试使用 is_object() 甚至确定它的类。在 PHP7 中,您将能够通过捕获 (Error $e) 来捕获该特定错误。
  • 感谢您的解释!
猜你喜欢
  • 2015-04-09
  • 1970-01-01
  • 2017-10-13
  • 2014-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-16
相关资源
最近更新 更多