Laravel Tutorials

Handling Exceptions with Laravel's rescue Helper

Published
Handling Exceptions with Laravel's rescue Helper image

Laravel's rescue function provides an elegant approach to managing potential exceptions in your code, allowing operations to continue gracefully even when errors occur.

The function accepts several parameters for flexible implementation:

// Execute code with default null fallback
return rescue(function () {
    return $this->method();
});

// Specify a default value
return rescue(function () {
    return $this->method();
}, false);

// Use a fallback closure
return rescue(function () {
    return $this->method();
}, function () {
    return $this->fallbackMethod();
});

This approach proves particularly valuable when working with external services:

class ApiService
{
    public function fetchAccountData($accountId)
    {
        return rescue(function () use ($accountId) {
            $response = Http::timeout(3)->get("api.external.com/accounts/{$accountId}");
            $response->throw();
            return $response->json();
        }, [
            'id' => $accountId,
            'name' => 'Unknown',
            'status' => 'error',
            'last_checked' => now()
        ]);
    }
    
    public function processReports()
    {
        return rescue(
            function () {
                $reports = $this->getReports();
                $processed = [];
                
                foreach ($reports as $report) {
                    $processed[] = $this->processReport($report);
                }
                
                return $processed;
            },
            function () {
                Log::warning('Report processing failed, using cached data');
                return Cache::get('last_successful_reports', []);
            }
        );
    }
}

The rescue function enhances code reliability by providing built-in error recovery mechanisms without complicating your application's logic with extensive try-catch blocks.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
PhpStorm 2026.3 Adds an Agent Workspace and PHP 8.6 Support image

PhpStorm 2026.3 Adds an Agent Workspace and PHP 8.6 Support

Read article
Insert Middleware Before or After Existing Laravel Middleware image

Insert Middleware Before or After Existing Laravel Middleware

Read article
Laravel Herd 2: Coding Agents, Workspaces, and Cloud Deploys image

Laravel Herd 2: Coding Agents, Workspaces, and Cloud Deploys

Read article
Strict Request Input Casting in Laravel image

Strict Request Input Casting in Laravel

Read article
Prevent Duplicate File Uploads With Laravel Storage Dedupler image

Prevent Duplicate File Uploads With Laravel Storage Dedupler

Read article
Slow Job Alerts and Replica Commit IDs in Laravel 13.40 image

Slow Job Alerts and Replica Commit IDs in Laravel 13.40

Read article