Mahmoud Al-HabashOpen to new projects
  • WorkWork
  • ServicesServices
  • LabLab
  • WritingWriting
  • ContactContact
Amman--:--عربي
Writing/Laravel

Laravel Exception Handling Best Practices

Handle exceptions gracefully with custom exception handling.

16 January 20261 min readLaravelاقرأ بالعربية

On this page

  1. Custom Exception
  2. Report Exception
  3. In Handler
  4. Don't Report

Handle exceptions properly:

Custom Exception

class PaymentFailedException extends Exception
{
    public function render($request)
    {
        return response()->json([
            'error' => 'Payment failed'
        ], 422);
    }
}

Report Exception

php
public function report()
{
    Log::error('Payment failed', ['user' => auth()->id()]);
}

In Handler

php
public function register()
{
    $this->reportable(function (PaymentFailedException $e) {
        // Custom reporting
    });
}

Don't Report

php
protected $dontReport = [
    ValidationException::class,
];
All articles

Keep reading

  • Laravel Action Classes Pattern16 January 20261 min read
  • Laravel Action Classes: Single Responsibility16 January 20261 min read
  • Laravel API Rate Limiting16 January 20261 min read