Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

Catching Exceptions

Hello,

I have a problem with a PDF parser.

I have an error with secured PDF files and I want to catch the exception.

$parser = new \Smalot\PdfParser\Parser();

foreach ($documents as $document) {

    try {

        $pdfFile = $parser->parseFile(storage_path('app/'.$document->fichier));

    } catch (Exception $e) {

        //

    }

}

But when I run this code, it generates an exception which is not catched and the program stops.

Secured pdf file are currently not supported.

What's wrong with my try ... catch ?

Is there another way to catch an exception with Laravel ?

Thanks for your help.

Vincent

0 likes
6 replies
sr57's avatar

You should be able to do st like this

} catch (Exception $e) {
      if (  'Secured pdf file are currently not supported.' === $e->getMessage() ) {
	        // do what you want 
	} else {
            throw $e;
   }
}

vincent15000's avatar

Thank you ... the problem is that the code in the catch section is not executed.

vincent15000's avatar

The exception is thrown by the smalot package.

    public function parseContent($content)

    {

        // Create structure from raw data.

        list($xref, $data) = $this->rawDataParser->parseData($content);

 

        if (isset($xref['trailer']['encrypt'])) {

            throw new \Exception('Secured pdf file are currently not supported.');

        }

 

        if (empty($data)) {

            throw new \Exception('Object list not found. Possible secured file.');

        }

		...

vincent15000's avatar

Thank you ... the soluce is to add use Exception; at the top of the controller. I thought it wasn't necessary because it's native in PHP. Thanks ;).

sr57's avatar

You are welcome.

Is there another way to catch an exception with Laravel ?

Laravel add good functionalities and conventions but does not change php behaviour.

1 like

Please or to participate in this conversation.