Laravel 9 Work with Exception Handling Example Tutorial

Share this Article
Reading Time: 4 minutes
348 Views

Inside this article we will see the concept i.e Laravel 9 work with exception handling. Article contains the classified information about exception handling in laravel 9. Article is super easy to understand.

An exception is an object that describes an error or unexpected behaviour of a PHP script. Exceptions are thrown by many PHP functions and classes. User defined functions and classes can also throw exceptions.

We will use the exact same concept to handle exception in laravel as same as PHP. In PHP we have an Exception class which provides methods to get error message or exceptions including error code, etc. We implement this exception concept inside try catch block of PHP.

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

$ composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

$ php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Exception Try Catch Block

We have a special syntax in PHP to write try catch block to handle exceptions.

try {

  /* Write Your Code Here */

} catch (Exception $e) {

    return $e->getMessage();
}

In try{} block we write those sections of code which seems to give any kind of exception. To handle those exceptions which generates try{} block handled by catch{} block.

Try Block

The try block contains the code that may potentially throw an exception. All of the code within the try block is executed until an exception is potentially thrown. Throw: The throw keyword is used to signal the occurrence of a PHP exception.

Catch Block

 A “catch” block retrieves an exception and creates an object containing the exception information.

Laravel Handle Exception

We will consider a small program where we write a code inside any of your controller. That code will have some kind of exceptions.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Exception;

class HomeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        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);
    }
}

When we execute this code via route.

It will give exception messages.

We hope this article helped you to Laravel 9 Work with Exception Handling Example Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5