Laravel 10 Try Catch All Exceptions

try {
// code that may throw an exception
} catch (\Exception $e) {
// handle the exception
}

Laravel 10 try catch statement example. In this tutorial, we will show you how to use try-catch statements with Laravel 10 in the controller, model, events, etc for error handling with all exceptions.

But sometimes a user searches for such a product. Which is not in your database. And you have not imposed any condition on the controller not getting the product. So at this time the user is not see anything on the search page or see something wrong in the error. So in this situation, you can find the error by using Try-Catch and show the correct information to the user.

The syntax represents the try..catch statement:

try {
    // run your code here
}
catch (exception $e) {
    //code to handle the exception
}

The try…catch statement is used to handle the errors.

Let’s take a look example of laravel try-catch:

Let’s take a look a very easy example, Here you have product table and find the product with it’s title.

You can handle errors using try…catch statement in laravel. See the following representation of try..catch.

If you are not checking for product exist or not in database, And you can directory pass data to your blade file.

So there has two case, first one is if the product is found, there were no errors, but if a product is not found, there will be display some errors on your search.blade.php file.

Go to .env file and set APP_DEBUG=false and then the browser will just show blank Whoops, looks like something went wrong. But that still doesn’t give any valuable information to our visitor.

If the product is not found and error occurs, so you can pass errors on your search.blade.php file with try..catch statement.

See the following example:

public function search(Request $request)
{
    try {
        $product = Product::where('title',$request->get('title'));
    } catch (ModelNotFoundException $exception) {
        return back()->withError($exception->getMessage())->withInput();
    }
    return view('product.search', compact('product'));
}

If you want to display an error in Blade file, you can do look like:

<h3 class="page-title text-center">Search by proudct title</h3>

@if (session('error'))
<div class="alert alert-danger">{{ session('error') }}</div>
@endif

<form action="{{ route('product.search') }}" method="POST">...</form>

Conclusion

In this laravel tutorial, you have learned how to use try catch statment in Laravel 10 application.

Recommended Laravel Posts

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?