Lesson 17 โข Advanced
Advanced Error Handling ๐จ
Build custom exception hierarchies, global error handlers, structured logging, and environment-aware error reporting.
What You'll Learn in This Lesson
- โข PHP's Throwable hierarchy: Error vs Exception
- โข Build custom exception classes with HTTP status codes
- โข Catch specific exception types for granular error handling
- โข Set up global exception and error handlers
- โข Structured logging with JSON for production monitoring
Custom Exception Hierarchies
Instead of throwing generic Exception everywhere, create a hierarchy of custom exceptions. This lets you catch specific error types and respond appropriately โ returning 404 for not-found errors, 422 for validation failures, and 500 for unexpected crashes.
Try It: Exception Hierarchy
Build and catch custom exceptions with status codes and specific error types
// Custom Exception Hierarchies in PHP
console.log("=== Exception Hierarchy ===");
console.log();
console.log("PHP's built-in hierarchy:");
console.log(" Throwable");
console.log(" โโโ Error (fatal โ don't catch these normally)");
console.log(" โ โโโ TypeError");
console.log(" โ โโโ DivisionByZeroError");
console.log(" โโโ Exception (your code should throw these)");
console.log(" โโโ RuntimeException");
console.log(" โโโ InvalidArgumentException");
console.log(" โโโ Logic
...Global Handlers & Logging
A global exception handler catches anything that slips through your try-catch blocks. Combined with structured JSON logging, it ensures no error goes unnoticed and your production logs are machine-parseable for monitoring tools like ELK Stack, Datadog, or Sentry.
Try It: Global Handler & Logging
Set up global handlers, configure error reporting, and build structured loggers
// Global Error Handler & Logging
console.log("=== Global Exception Handler ===");
console.log();
console.log("PHP lets you set a global handler for uncaught exceptions:");
console.log();
console.log(" set_exception_handler(function(Throwable $e) {");
console.log(" // Log the error (never show details to users!)");
console.log(" error_log($e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine());");
console.log(" ");
console.log(" // Show user-friendly error page");
c
...โ ๏ธ Common Mistakes
catch (Exception $e) swallows everything. Catch specific types first, then fall back to a generic handler.$e->getTraceAsString() in production. Log it, but show users a friendly message.catch (Exception $e) {} silently swallows errors. At minimum, log the exception.match expression in your global handler to map exception types to HTTP responses cleanly.๐ Quick Reference โ Error Handling
| Concept | Purpose |
|---|---|
| set_exception_handler() | Catch uncaught exceptions globally |
| set_error_handler() | Convert PHP errors to exceptions |
| error_reporting(E_ALL) | Report all errors (dev mode) |
| error_log() | Write to PHP error log file |
| Custom exceptions | Type-specific error handling with status codes |
| try / catch / finally | Handle errors with guaranteed cleanup |
๐ Lesson Complete!
You can now handle errors like a pro! Next, learn to consume REST APIs with cURL, Streams, and Guzzle.
Sign up for free to track which lessons you've completed and get learning reminders.