PHP Exceptions: An Overview


Exceptions are a way to modify the flow of an application based on specific errors that occur during execution. They are used by developers to prevent invalid values or configurations from being executed.  There are 2 way to handle Exceptions in PHP by the user of Try Catch blocks or the use of a global exception handling method, in most cases you will want to utilize the Try Catch method. Below we will outline these 2 methods but first lets review the Base Exception class.

Understanding The Basics

The Base Exception Class

The built in Exception Class to PHP comes set-up with a lot of functionality to obtain information on where and why the exception occurred within the script or application. The main element to an exception is the message which if used properly should give the details as to why the exception was raised.  In addition the Exception class has a code variable for user defined values as well as providing handles to the line and file that the exception occurred in.  To instantiate a new Exception a user can simply instantiate the Exception by passing the message, code, and optional root exception all of which are optional.

$exception = new Exception("test", 304, $baseException );

Extending an Exception

The real power of extensions comes from the ability to extend the base class to define Custom Exceptions that contain more meaningful information.  Doing so allows for the handler to execute different code based on the Exception type.  In addition it allows for you to add properties and methods to the Exception that can be used by the handlers.  To create a custom exception one simply needs to extend off of the Exception class.

class CustomException extends Exception { }

When creating exceptions be sure that if you overwrite the constructor that you call the parent constructor so as to not loose any base functionality.

parent::__construct($message, $code, $previous);

Raising an Exception

Raising or Throwing an Exception is down by the throw keyword in PHP, when executed this will stop script execution and the Exception will be thrown up the processing stack until it is handled by the appropriate handler whether a global handle method or a catch block.

throw new CustomException("Invalid Value");

The Try Catch Block

The first method I will talk about in how to handle exceptions is the Try Catch Block.  The Try Catch Block is a construct used to surround code that could potentially throw exceptions and defines handlers for catching any exceptions that allow the developer to define how the script/app should continue in such an event. When an exception is raised the code stops executing within the try block and will resume in the appropriate catch block, after the catch block executes execution will resume after the last catch statement.  The basic format for this construct is defined as:

try {
...  // potentially dangerous code
} catch ( CustomException $e ) {
... // handle Exceptions of Type CustomException
} catch (Exception $e ) {
... // handle Exceptions of Type Exception

Reviewing the above construct you will see that we wrap any code that has the potential to raise an exception by the try clause. Then we define catch clauses for handling the Exceptions, an important thing to note is that you can define multiple catch statements so that you can handle different Types of exceptions in different manors. With saying that it is important to remember that the first catch block that matches the Exception Type will handle the exception so if you want to handle a CustomException different then other Exceptions it’s catch block needs to come first or the Exception catch statement will handle it as CustomException base type is Exception.

Using the above construct you will be able to catch exceptions thrown and handle them in the manor that best fits your application. Some examples used could be to display an error message to users telling them the functionality is defined, displaying alternate content, or quitting the application or script altogether.

Defining a Global Exception Handler

Unlike other programming languages such as Java and Flash, PHP allows the developer to define a global method that handles any uncaught exceptions. To define an exception handler you utilize the set_exception_handler function and pass it the name of the function to be used.

function handleException($exception) {

set_exception_handler( "handleException" )


Exceptions can provide valuable information when both developing applications and during the execution of an application or script by an end user.  If used correctly Exceptions are a great tool that can be used to create bug free applications. For further information you can view the PHP Exception Class page or the set_exception_handler page

// PHP //

Comments & Questions

Add Your Comment