Exceptions are the mechanism to catch some invalid behavior and gracefully handle them instead of just showing "500 server error" to the users.
Take a look at this example:
public function __invoke(){ try { $this->import($data); } catch (ImportHasMalformedData $e) { return redirect()->route('dashboard')->with('message', 'Uh oh, the import has failed. Try again later or contact support'); } return 'Imported';}
This snippet means that in case of ANY Exception, the user would get redirected back and see the error message.
But what if we're expecting something specific to happen?
Catching a Specific Laravel Exception
Try-catch is way more helpful when catching a specific Exception. Laravel and 3rd party packages often offer...
In the first example provided, you use the method import ($data)
$this->import($data), however, in your last try catch statement, you useImportController::importData(). Is there a connection between the 2 import methods? Does this mean that theImportControllerhas a static method calledimportData()and a regular method calledimport($data)? Are they the same thing (does laravel 'know' a method + a parameter can be directly called as methodParameter())? Or is this a typo?No, it's a basic method in this case, not static. This part is taken from the Handling Exceptions and Errors in Laravel so better refer there for more about exceptions.
Thank you very much!