Exception Handling in PHP

The try-catch

The primary method of dealing with exceptions in PHP is the try-catch. In a nutshell, the try-catch is a code block that can be used to deal with thrown exceptions without interrupting program execution. In other words, you can "try" to execute a block of code, and "catch" any exceptions that are thrown.

Catching all exceptions

The simplest way to catch exceptions is through the use of a generic try-catch block. Because exceptions are objects, they all extend a built-in Exception class (see Throwing Exceptions in PHP), which means that catching every exception thrown is as simple as type-hinting the global exception object, which is indicated by adding a backslash in front:

try {
  // ...
} catch ( \Exception $e ) {
  // ...
}

Catching specific exceptions

While catching every exception thrown is great for simplistic implementations—such as generalizing API error responses—best practice is to catch for specific exceptions. By using the same type-hinting method shown above in reference to specific exception objects instead of a global exception object, you can react more effectively to individual problems that your application may encounter on the way.

try {
  // ...
} catch ( \Custom\Exception $e ) {
  // ...
}

Catching just one type of exception isn't very valuable though, is it? What happens if your application throws a different exception? Much like an if-elseif-else chain, a try-catch block can have multiple catches—which, when combined with a check for the built-in exception class, allows you to logically adapt to any and all issues that may arise:

try {
  // ...
} catch ( \Custom\Exception $e ) {
  // ...
} catch ( \Other\Exception $e ) {
  // ...
} catch ( \Exception $e ) {
  // ...
}

The exception handler

While wrapping dangerous code in try-catch blocks is a great way to harden an application against unexpected errors, you can't always catch everything. Sometimes an exception falls through the cracks, which is where the global exception handler comes into play. This method, dubbed setexceptionhandler, provides a fallback for any uncaught exceptions.

set_exception_handler(function($exception) {
  // ...  
});

Although you can utilize this method in a number of different ways, it is generally best used for logging and display formatting, as every exception thrown will be caught—not just built-in exceptions—which can result in unpredictable behavior if not used properly.

Framework exception handling

While the global exception handler can be used regardless of framework, it is important to know that frameworks like Symfony and Laravel have their own ways of handling exceptions. Laravel, for example, handles all exceptions using a class that defines how exceptions should be reported and rendered. Symfony, on the other hand, uses an event listener to catch exceptions. Both valid ways to handle exceptions, but designed to fit within their own ecosystems.

Check out our blog to see a working example of Laravel error reporting.