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

rene's avatar
Level 2

Catch FileNotFoundException?

How to Catch the Exception?:

[Illuminate\Contracts\Filesystem\FileNotFoundException]
 images/foobar/NatGeo05.jpg

  [League\Flysystem\FileNotFoundException]
  File not found at path: images/foobar/NatGeo05.jpg

I'm doing:

// assume $photo works 
               try {
        // fails here
                    Storage::get($photo->name);
                }
                catch (Exception $e) {
                    $photo->delete(); // doesn't come here
                }
0 likes
4 replies
bestmomo's avatar

You can do that in Exceptions\Handler :

public function render($request, Exception $e)
{
    if($e instance of \Illuminate\Contracts\Filesystem\FileNotFoundException)
    {
        // Do something
    }
    return parent::render($request, $e);
}
2 likes
ortejim's avatar

Here is what worked for me:

   use Illuminate\Contracts\Filesystem\FileNotFoundException;  //at the top of your script

try {
        $file = Storage::get($photo->name);
    } catch (FileNotFoundException $e) {
        abort(404); //or whatever you want do here
    }
        
1 like

Please or to participate in this conversation.