PHP:
Handling errors

How to:

In PHP, you can manage errors using try-catch blocks, and you can customize the process with custom error handlers and exceptions.

// Basic try-catch example
try {
  // Do something risky
  $file = fopen("nonexistentfile.txt", "r");
} catch (Exception $e) {
  // Handle the error
  echo "Error: " . $e->getMessage();
}

// Setting a custom error handler
set_error_handler(function($severity, $message, $file, $line) {
  throw new ErrorException($message, 0, $severity, $file, $line);
});

// Using exceptions
class MyException extends Exception {}

try {
  // Do something and throw a custom exception
  throw new MyException("Custom error!");
} catch (MyException $e) {
  // Handle the custom exception
  echo $e->getMessage();
}

// Sample output:
// Error: fopen(nonexistentfile.txt): failed to open stream: No such file or directory
// Custom error!

Deep Dive

Back in the day, PHP errors were more about warnings and notices that didn’t stop script execution. As the language matured, it adopted more robust object-oriented error handling via the Exception class introduced in PHP 5. Later, PHP 7 came out with Error classes that finally differentiated between errors and exceptions.

Before try-catch blocks, PHP used set_error_handler() to deal with errors. try-catch is cleaner, more modern. But custom error handlers still have a place, especially for legacy code or when you need to catch what would normally be non-exception errors.

The Throwable interface in PHP 7+ means whether it’s an Error or Exception, you can catch both. This is handy because now you don’t miss critical runtime errors, which were harder to track before.

Alternatives outside PHP’s built-in mechanisms include libraries and frameworks that come with their own error handling systems, offering more features like error logging to files or displaying user-friendly error pages.

See Also