DEV Community

Evgeny Musonov
Evgeny Musonov

Posted on

How can i catch exceptions in a few methods?

Hi everyone!
I have a PHP class for file upload and methods for upload and resize, which return an object of the class.
How i call methods in a controller:
$imageUploader->upload();
or
$imageUploader->upload()->resize();

Each method contains some exceptions. Right now i catch exceptions in each method, what is not correct. I don't want to catch it in the controller. So how can i catch all exceptions in one place?

Top comments (2)

Collapse
 
brandinchiu profile image
Brandin Chiu

Depending on what you're trying to do, you can either make use of Laravel's "middleware" (laravel.com/docs/5.8/middleware), or Laravels built-in exception controller (laravel.com/docs/5.8/errors)

There isn't always necessarily a "right" way to do exception handling though. It really depends on your application. The above options are some of the more common systems, though.

Collapse
 
evmusonov profile image
Evgeny Musonov

Yes, i found docs about reportable & renderable Exceptions in Laravel, that's what i need, thank you