laravel try catch error handling (exception in controller)

Today, We want to share with you laravel try catch.In this post we will show you Laravel Exceptions: How to Catch, Handle and Create Your Own, hear for PHP Laravel Exception Handling we will give you demo and example for implement.In this post, we will learn about Php Try Catch Block Exception Handling with an example.

Introduction To Exception Handling in Laravel

in Laravel, All exceptions are handled by the App\Exceptions\Handler class.

List of available built-in exception classes since PHP 7.4:

You are inside a namespace so you should use \Exception to specify the global namespace:

try {

  $this->buildXMLHeader();

} catch (\Exception $e) {

    return $e->getMessage();
}

OR

App\Services\PayUService\Exception
getMessage();
    echo "";
    echo "getCode(): " . $e->getCode();
    echo "";
    echo "__toString(): " . $e->__toString();
}
?>

The entire handling process is wrapped inside a try…catch:

public function store(Request $request)
 {  
    try   
    {  
        $product = new PostComment();  
        $product->content = $request['product'];  
        $product->user_id = Auth::user()->id;  
        $product->product_id = $request['productId'];  
        $product->save();  
        $product = Product::where('id', '=', $product->product_id)->first();  
        $product->updated_at = $product->created_at;  
        $product->update();  
    }    
    catch(Exception $e)  
    {  
        if (!($e instanceof SQLException)) {
            app()->make(\App\Exceptions\Handler::class)->report($e); // Report the exception if you don't know what actually caused it
        }
        request()->session()->flash('unsuccessMessage', 'Failed to add product !!!');  
        return redirect()->back();  

    }  
    request()->session()->flash('successMessage', 'Comment has been successfully added !!!');  
    return redirect()->back();  
}  

Example 2

try {
    App\Models\Product::find(1);
} catch (\Exception $ex) {
   dd('Exception block', $ex);
} catch (\Throwable $ex) {
   dd('Throwable block', $ex);
}

Error handling with try and catch in Laravel

In Laravel, you can use the try and catch blocks for error handling. This is useful when you have code that may throw an exception or error that you want to handle in a specific way.

Here is an example of using try and catch blocks in Laravel:

try {
    // Code that may throw an exception or error
} catch (Exception $e) {
    // Code to handle the exception or error
}

In the above code, the try block contains the code that may throw an exception or error. If an exception or error is thrown, the catch block will catch it and execute the code inside it.

You can replace Exception with any specific exception class that you want to catch. For example, if you want to catch a PDOException exception, you can modify the code as follows:

try {
    // Code that may throw a PDOException exception
} catch (PDOException $e) {
    // Code to handle the PDOException exception
}

You can also catch multiple exceptions by separating them with pipes |. Here is an example:

try {
    // Code that may throw a specific set of exceptions
} catch (Exception1 | Exception2 $e) {
    // Code to handle Exception1 or Exception2
}

Finally, you can add a finally block that will be executed regardless of whether an exception is thrown or not. Here is an example:

try {
    // Code that may throw an exception or error
} catch (Exception $e) {
    // Code to handle the exception or error
} finally {
    // Code that will be executed regardless of whether an exception is thrown or not
}

In the above code, the finally block will be executed regardless of whether an exception is thrown or not. This is useful for cleaning up resources or performing other actions that need to be done regardless of the outcome of the try block.

I hope you get an idea about Laravel – Error & Exception Handling.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment