大家好! 在我的 Laravel 应用程序中,我有一个 Excel 文件的上传功能。我从网上获得了这段代码并将其调整到我的应用程序中。问题是,它没有捕捉到用户提交文件但尚未选择文件时产生的 fatal error 异常。我不明白为什么它没有被捕获。我将添加我的 Controller 的一部分。
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) {
否则,您正在使用您的类所在的任何 namespace ,并试图捕获在该 namespace 中声明的异常。看起来您的 ErrorException 设置类似。
我不确定上面的 namespace 是否是您的类派生的 namespace ,我只是猜测这是因为您使用的是 Laravel。
关于php - 捕获 FatalErrorException PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40605363/