Exceptions
Laravel translates uncaught exceptions into HTTP responses.
What it is
Laravel translates uncaught exceptions into HTTP responses.
When to use it
Render known domain failures safely and report unexpected failures.
Syntax
// bootstrap/app.php, inside withExceptions:
$exceptions->render(function (InsufficientFunds $e, Request $request) {
return response()->json(["error" => "insufficient_funds"], 409);
});Example
// A domain exception:
class InsufficientFunds extends RuntimeException {}
// Application rule:
if ($balance < $amountCents) {
throw new InsufficientFunds("Insufficient funds");
}
// Let unexpected exceptions reach Laravel reporting.Common mistakes
Do not return $e->getMessage() for unknown failures; APP_DEBUG must be false when handling sensitive data.