2017-04-03 137 views
1

当试图捕获/处理从PDFParser抛出的异常时,我无法捕捉它。我使用了一个简单的try catch语句,如下所述。PHP异常未被捕获,laravel,流明

try{ 
    $pdf = $parser->parseFile($filepath); 
    $text = $pdf->getText();  
} catch(\Exception $e){ 
    $text = $paper->abstract; 
} 

引发异常如下。

if (empty($data)) { 
     throw new \Exception('Object list not found. Possible secured file.'); 
} 

The output is here.

lumen.ERROR: Exception: Object list not found. Possible secured file. in

/Users/pietrosette/Documents/CS_310/AcademicWordCloud-Backend/vendor/smalot/pdfparser/src/Smalot/PdfParser/Parser.php:98 Stack trace:

#0 /Users/pietrosette/Documents/CS_310/AcademicWordCloud-Backend/vendor/smalot/pdfparser/src/Smalot/PdfParser/Parser.php(74): Smalot\PdfParser\Parser->parseContent('%PDF-1.5\r%\xE2\xE3\xCF\xD3\r...')

#1 /Users/pietrosette/Documents/CS_310/AcademicWordCloud-Backend/app/Http/Controllers/ACMServer.php(198): Smalot\PdfParser\Parser->parseFile('/Users/pietrose...')

回答

0

你有一个错字在你的陷阱:

try{ 
    $pdf = $parser->parseFile($filepath); 
    $text = $pdf->getText();  
} catch(\Execption $e){ 
    $text = $paper->abstract; 
} 

“异常” 拼写错误。

+0

感谢您的编辑,但它仍然无法正常工作。 –

+0

这仍然是一个错误吗? – suecarmol

+1

是的,我最终将其更改为: if($ pdf ===“PDF> = 1.5”){ $ text =“paper-> abstract”;其他{ } $ text = $ pdf-> getText(); } 与 公共函数parseFile($文件名) { $含量=的file_get_contents($文件名)的一个包装; $ return = @ $ this-> parseContent($ content); if($ return === false){ return“PDF> = 1.5”; } else { return $ return; } } –

0

我写了一个包装并返回一个不同的值。这结束了工作,因为它没有封装在try/catch块中,所以实际上速度更快。

public function parseFile($filename) 
    { 
     $content = file_get_contents($filename); 
     $return = @$this->parseContent($content); 
     if($return === false){ 
      return "PDF >= 1.5"; 
     } else{ 
      return $return; 
     } 
    } 


    public function parseContent($content) 
    { 
     ... 

     if (isset($xref['trailer']['encrypt'])) { 
      return false; 
     } 

     if (empty($data)) { 
      return false; 
     } 
     .... 
    } 

这导致我的功能修改如下。

$pdf = $parser->parseFile($filepath); 

    if($pdf === "PDF >= 1.5"){ 
     $text = $abstract; 
    } else { 
     $text = $pdf->getText(); 
    }