January 28, 2012 learn_php

Exception Handling in PHP

Exception Handling in PHP

In special cases during a development, there will be the need to handle errors of some sort. PHP5 has introduced this new feature to handle exceptions with the Exception Class, and on this post, we will learn what the basics are for error handling with this exception handling class.

In many cases, us developers tend to struggle and have a hard time when it comes to: “What do I do if an error occurs somewhere?”. Now days, most languages support what we know as exception handling. This involves using a language construct known as: try/catch/throw. The idea behind all of this try/catch/throw code is that, there will be some code executed between a try and a catch. So basically, it is saying, “Hey! I am going to ‘TRY’ to run this code, but if I can’t run it, I will ‘THROW’ and exception. When these exceptions are ‘thrown’, something has to be there to catch it. That’s where the ‘CATCH’ comes in place. The basic structure of a try/catch code will look like this:

In the previous code, there is still something missing. That happens to be the keyword ‘THROW’. In cases like Java, these throw/catch are automated, but that is not the case with PHP and have to be written by the developer. The way this ‘THROW’ is implemented is as follows:

We see that the exception construct expects 2 parameters. The first is the ‘error’ message that we want to get through to be displayed. The second can be a number code that you would like to assign to this error. In try/catch, there can be multiple catches, and that is good if you are targeting different situations.

When ‘throwing’ an exception, this process will go straight to the first catch. If if finds it matches the class that was thrown, then it will enter that catch block, if not, then it will keep looking for catch blocks that have been implemented, until it finds the right one. Now, the above catch statement is still incomplete, let’s go ahead and finish it by catching what was thrown.

Notice that we declared an object of type exception. Now, inside these blocks, you are able to invoke functions that are available to us as programmers. Let’s go ahead and finish the error statement for the user, by pretending that we are building a function to do division, and as we all know, there can be no division by zero.

Full list of methods

The following is a list of some usable methods that are available to you as a developer:


Creating your own exception class

Note from the list above, that most, if not all methods are public, but final, which means you can’t change the behaviour of this class. However, you can implement your own class, inheriting from this exception class to be able to display your own error messages, as the method __toString() is the only one that can be changed. Let’s create our own exception class and modify the __toString() method to display a nicer looking exception.

So now, if we wanted to invoke this exception, instead of a regular PHP exception, then we would do it the same way as a regular one, but with its name.

Indeed that this is very simple, and may look like the created class does nothing, and yes, I’d have to agree, but this is just a mere sample of what can be done with exceptions. In some cases, you may need to write to a log file, or even save exceptions into a database for monitoring later on. Whatever reason it is, it is sure to ease up some of your code, and make it reusable. (The whole point of OOP in PHP).


What are the advantages with using exceptions in PHP? One would use these exceptions to cover some big blocks of code that you are most certain any type of error can occur. When using exceptions, the need to write if statements decreases, thus making your code look even cleaner, and readable.

4 thoughts on “Exception Handling in PHP

  1. I use a combination of both exception handling and trigger_error.

    Remember, exceptions should be used in a case where the code can no longer process because a required component or value is missing; normally, an uncaught exception will terminate the rest of the script,
    if the case were you weren’t able to make a database connection, or a SQL insert failed for which you needed the id as a foreign key value for subsequent inserts, then you may want to stop the rest of the script.

    In some instances we can continue the code as normal, so make sure you know what it is you’re trying to accomplish. If you want to display an error if something fails internally, you can use the trigger_error function which will throw an E_USER_NOTICE, but the rest of the script will continue even if there is no try/catch blocks.

    For some debugging, I use trigger_error instead.

    Also if you’re throwing your own exceptions, I’d recommend using the set_error_handler () and set_exception_handler() to define how to handle uncaught exceptions.

Leave a Reply

Your email address will not be published. Required fields are marked *

The qTranslate Editor has disabled itself because it hasn't been tested with your Wordpress version yet. This is done to prevent Wordpress from malfunctioning. You can reenable it by clicking here (may cause data loss! Use at own risk!). To remove this message permanently, please update qTranslate to the corresponding version.