Handling Exceptions and Errors in Laravel 12
Quite often, I get questions like these: When do we need to use the try/catch blocks in Laravel? Wh...
We found 12 results for "exceptions".
Quite often, I get questions like these: When do we need to use the try/catch blocks in Laravel? Wh...
The try-catch PHP operator is very old, and we all know its syntax. But the confusing part is WHEN to use it. In what cases? In this tutorial, I will show practical examples to explain the answer.
API-based projects are more and more popular, and they are pretty easy to create in Laravel. But one topic is less talked about - it's error handling for various exceptions. API consumers often complain that they get "Server error" but no valuable messages. So, how to handle API errors gracefully? How to return them in "readable" form?
Quite often web-developers don't care enough about errors. If something goes wrong, you often see default Laravel texts like "Whoops, something went wrong" or, even worse, the exception code, which is not helpful at all to the visitor. So I decided to write a step-by-step article of how to handle errors in elegant way and present proper error information to the visitor.
I see a lot of developers not using Exception classes and try-catch blocks because they don't understand how they actually work under the hood. So, let's learn with an example.
Today I want to talk about Exceptions in Laravel, specifically with the example of API requests and the 404 Not Found case. How to override the non-ideal error message?
Try-catch is a typical PHP/Laravel way to process the exceptions. But how do you handle PHP errors? Or, do you? Let me show you a few examples.
In Laravel 11, there's a new place where you define/configure your Middleware and Exception classes.
Laravel Validation returns an error as a JSON response with the error messages in a strict standard format. What if you want to change it to an entirely different structure because your front-enders ask you for specific key-value pairs?
Laravel has a great database migration mechanism, but there are some things missing there. For example, what if you want your IDs start with some number, like 140001, 140002 etc?
This is a free video from my online course "Excel: Export/Import in Laravel". In this video, I'm trying to import a file via Laravel Excel package, and showing how to handle errors and catch PHP Exceptions.