Handling errors and exceptions in PHP

November 22, 2022

A fatal error will cause a program to crash and a web page will not be rendered (generated), and usually we won’t see an error message in the response. But we can write code that implements different reactions to such errors, and we can call this piece of code an exception handler.

An exception is technically an object created by the PHP runtime when any fatal error occurs. This object stores information about it. Not all fatal errors throw exceptions!

Errors that occur when compiling the code, specifically when resolving constants and performing includes using require and require_once constructs, do not throw exceptions.

Non-fatal errors don’t necessarily throw exceptions either!

An exception can also be thrown by a programmer to notify the application of any abnormal situation.

Exception handling therefore comes down to writing a piece of code that performs some actions in order to either eliminate the consequences of an error or to notify the site visitor about it (for example, displaying a nice error page with a message).

Exception handlers

Exception handlers look like this:

try {
 // try block - in this block an exception might be thrown
} catch (exception type 1 $e) {
  // catch block - if we get exception of type 1
} catch (exception type 2 $e) {
  // another catch block - if we get exception of type 2
} finally {
  // this block will be executed always, regardless of whether exceptions took place

Note that exception objects $e in catch blocks are only accessible within these blocks.

An example:

try {
   echo 24 % О; // we intentionally divide by 0 so that PHP thorws an error  
} catch (DivisionByZeroError) {
   echo "You are dividing by zero!!"; // We know PHP calls this DivisionByZeroError
   echo $e->getMessage(); // Every exception object has this method, it will give you details
   // about the error that happened
} finally {
   echo 'We reached the end'; // This block will execute anyway

Custom exception classes

You can create your own exception classes by extending the base exception class in PHP:

class Page404Exception extends Exception {}

In most cases developers don’t even need to implement any extra methods or properties, they just need a new unique exception class.

Throwing exceptions

Exceptions that are derived from Error class are only thrown by the PHP runtime itself. Exception classes declared by developers must be thrown by developers in the code.

Throwing an exception is performed by the throw language construct: throw <exception object>

When creating an exception object, the only optional parameter is a string with a message about the error that occurred.

An example of throwing a Page404Exception:

throw new Page404Exception('This web page does not exist');

An exception thrown in this way can be handled in the same way as any built-in PHP exception:

try {
  throw new Page404Exception(); 
} catch (Page404Exception $e) {
  echo "Oh it happened!";

Published by Artiphp who lives and works in San Francisco building useful things.