ItSolutionStuff.com

Laravel 9 Exception Handling Example Tutorial

By Hardik Savani • November 5, 2023
Laravel

In this example, you will learn laravel 9 exception handling. This tutorial will give you simple example of laravel 9 exception handler. you can understand a concept of laravel 9 error handling. This article will give you simple example of laravel 9 exception handling example. you will do the following things for laravel 9 try catch example.

Why do we need to use try-catch in our PHP laravel app? I will explain to you why. Sometimes we wrote code right but there is a possibility that if the user inputs the wrong data or something will wrong on the website then our user should not show an error, we can handle the error and display a proper message where is an issue and what they need to do. so same thing whenever you need to use try-catch then you can easily use like below example.

Let's see below syntax and example:

Syntax:

You have to use try catch as below syntax and you must need to use "use Exception" on top of file in laravel.

try {

/* Write Your Code Here */

} catch (Exception $e) {

return $e->getMessage();

}

Example:

Here, i will show you controller file code and we will use "$input" variable in controller method that not define any more. it will generate error. so let's see bellow example:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

use Exception;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function show($id)

{

try {

$user = User::find($input['id']);

} catch (Exception $e) {

$message = $e->getMessage();

var_dump('Exception Message: '. $message);

$code = $e->getCode();

var_dump('Exception Code: '. $code);

$string = $e->__toString();

var_dump('Exception String: '. $string);

exit;

}

return response()->json($user);

}

}

Output:

string(44) "Exception Message: Undefined variable $input"

string(17) "Exception Code: 0"

string(17) "Exception String: ......"

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel 9 Enum Model Attribute Casting Example

Read Now →

Laravel 9 Store JSON in Database Example

Read Now →

Laravel 9 Livewire Pagination Example Tutorial

Read Now →

Laravel 9 FCM Push Notification Example Tutorial

Read Now →

Laravel 9 React JS CRUD using Vite Example

Read Now →

Laravel 9 Dependent Dropdown Example Tutorial

Read Now →

Laravel 9 Livewire Form Submit Example

Read Now →

Laravel 9 Ajax File Upload with Progress Bar Tutorial

Read Now →

Laravel 9 Custom Validation Error Message Example

Read Now →

Laravel 9 Drag and Drop File Upload with Dropzone JS

Read Now →

Laravel 9 Cron Job Task Scheduling Tutorial

Read Now →

Laravel 9 Livewire CRUD using Jetstream & Tailwind CSS

Read Now →