ItSolutionStuff.com

How to use Try Catch Exception in Laravel App?

By Hardik Savani β€’ April 16, 2024
Laravel

Hi Dev,

This tutorial shows you laravel try catch in controller. you can see how to use try catch in laravel controller. this example will help you laravel try catch exception not working. I’m going to show you about try catch laravel example. Alright, let’s dive into the steps.

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, you can use this example in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions.

Let's see below syntax and example:

Syntax:

You have to use try catch as below syntax and you just need to use "use Exception" on top of the 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...

Tags: Laravel
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 Eloquent Model Custom Function Example

Read Now β†’
β˜…

Laravel Add Foreign Key to Existing Table with Data Example

Read Now β†’
β˜…

Laravel Blade Include File Example

Read Now β†’
β˜…

Laravel Eloquent updateOrCreate Example

Read Now β†’
β˜…

Laravel Carbon Subtract Year Example

Read Now β†’
β˜…

How to Send Email with Attachment in Laravel?

Read Now β†’
β˜…

Laravel Model Events Tutorial

Read Now β†’
β˜…

Laravel Livewire Image Upload Example

Read Now β†’
β˜…

How to Use MySQL View in Laravel?

Read Now β†’
β˜…

How to Group By with Order By Desc in Laravel?

Read Now β†’