PHP: Making an exception

Gavin Sherry continues his exploration of what makes PHP 5 so special. This month it's exceptional.

In the time between this and my last column, PHP 5 has moved out of beta and into stable release, which means that you can now begin using PHP 5 in production with the confidence that it's supported and well tested. This month, I'll be looking at one of the very exciting new features in PHP 5 - exceptions.

Python vs. PHP: Choosing your next project's language

What are exceptions?

An exception is an error that occurs during the course of the execution of a script. When an error occurs in PHP 4, control is passed to the error handler, which outputs an error message to the user. A few months ago we looked at how to customise this error handling with set_error_handler(). The problem with this system is that you often need to code your scripts in such a way that you test for errors at each possible occurrence, swap error handlers around to match the code context you are in and more. This can be a nightmare, particularly if your application is being developed by more than one person, since you need to spend a lot of time getting the error handling right.

Object orientated languages such as Java, however, support an in-line mechanism which allows you to handle exceptions in a much more systematic way. This mechanism is to surround whole code blocks with an exception handler which is able to deal with any error in that block or, alternatively, pass it on to another exception handler. The way this is done can be seen in here:

01 try

02 {

03 /* code here */

04 }

05 catch(Exception $e)

06 {

07 /* recovery code */

08 }

Line 1 uses the try construct to instruct PHP that it should trap exceptions resulting from the execution of the code block which follows. If an exception occurs, control is passed to the catch block. This block is intended to allow the programmer to provide error recovery. If the catch block cannot resolve the error, it has the option of 're-throwing' the error. Consider the example below:

01 function test()

02 {

03 try

04 {

05 /* code */

06 }

07 catch(exception $e)

08 {

09 throw $e;

10 }

11 }

12 try

13 {

14 test();

15 }

16 catch(Exception $e)

17 {

18 /* recover here */

19 }

In this case, we have a function test(). The function tries to execute some code and if an exception occurs, control is passed to the catch block at line 07. We pretend that this catch block is unable to handle the error and we re-throw the error on line 09 using the 'throw' construct. We pass to this the exception itself: $e. By wrapping the call to test(), on line 14, in an exception block, we are able to catch the re-thrown error on line 16 and deal with it there.

Extending exceptions

In PHP 5, we can also extend exceptions. When dealing with a complex object-oriented project, you will want to be able to handle class specific exceptions. One of the most common places where this occurs is in dealing with databases, since errors encountered with the database are often completely unrelated to those in PHP - see here:

01 class SQLException extends Exception

02 {

03 var $state;

04 function _ _construct($SQLState)

05 {

06 $state = $SQLState;

07 }

08 function state2msg()

09 }

10 /* map SQLState code to message */

11 }

12 }

13 function runsql($sql)

14 {

15 /* prepare and send SQL query */

16 if($sqlstate)

17 throw new SQLException($sqlstate);

18 }

19 try

20 {

21 /* code, including runsql() */

22 }

23 catch(SQLException $e)

24 {

25 ech $e->state2msg();

26 }

27 catch($e)

28 {

29 /* Catch all axceptions */

30 }

We now have a very sophisticated exception handling frame work. On line 01, we declare a class SQLException which extends the Exception class which is built into PHP 5. We declare on line 04 a constructor which saves the SQLState passed in at object creation time. We declare on line 08 a function which will map the numeric error code sent from our database into a user friendly message. On line 13 we create a function, runsql(), which can execute an SQL query on a database server. If it encounters an error on line 16, we throw an exception (line 17) being careful to pass in the SQL error code, $sqlstate.

In our main code body, starting at line 19, we run some code, including runsql(), in a try block. If an ex­-ception is generated by runsql(), or some other function which is able to throw an exception of type SQLException, then we catch it at line 23. We then call the state2msg() method of SQLException and tell the user the error we've encountered. We're also careful on line 27 to catch any other exceptions which are not of the type SQLException.

The Evolution of PHP

PHP was originally created by Rasmus Lerdorf in 1995. The project started as a set of tools to measure traffic to his resume. Rasmus continued to add functionality to these scripts and began implementing it in C. One of the most important aspects of this first implementation, PHP/FI, was its ability to interpret data submitted by forms.

With significant assistance from Andi Gutmans and Zeev Suraski, PHP/FI was transformed into PHP 3.0. This release was marked by real programming language features, a large extension framework, high performance and great documentation. Developers worldwide took notice and installations grew exponentially.

With PHP 3.0 released, Andi Gutmans and Zeev Suraski returned to the core of PHP - the Zend engine. Meanwhile, many other developers began contributing more and more extensions to PHP. The significant number of contributions led to problems however: different aspects of PHP were at different leaves of maturity and releases were made with experimental code included, since no one was able to test some extensions due to their obscurity and niche appeal.

PHP 5.0 addresses the shortcomings of the interpreter by building a robust object-oriented base with all the features of other languages, while integrating XML and other functionality which was previously unstable and inconsistent. The future is certainly bright for PHP and its users.

The home of PHP, and where you'll find more details on the new features to be found in PHP 5.0. The front page deals with major news and events, while you can find the complete source code for the latest version of PHP in the downloads section, with accompanying manuals also available in the documentation pages.

As you'd expect, there's a wealth of information available here, from upcoming user group events and training centres, to FAQs and tutorials. If you're new to PHP, try the Getting Started guide first - you'll find it at

Join the newsletter!

Error: Please check your email address.
Rocket to Success - Your 10 Tips for Smarter ERP System Selection
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Cool Tech

Breitling Superocean Heritage Chronographe 44

Learn more >

SanDisk MicroSDXC™ for Nintendo® Switch™

Learn more >

Toys for Boys

Family Friendly

Panasonic 4K UHD Blu-Ray Player and Full HD Recorder with Netflix - UBT1GL-K

Learn more >

Stocking Stuffer

Razer DeathAdder Expert Ergonomic Gaming Mouse

Learn more >

Christmas Gift Guide

Click for more ›

Most Popular Reviews

Latest Articles


PCW Evaluation Team

Edwina Hargreaves

WD My Cloud Home

I would recommend this device for families and small businesses who want one safe place to store all their important digital content and a way to easily share it with friends, family, business partners, or customers.

Walid Mikhael

Brother QL-820NWB Professional Label Printer

It’s easy to set up, it’s compact and quiet when printing and to top if off, the print quality is excellent. This is hands down the best printer I’ve used for printing labels.

Ben Ramsden

Sharp PN-40TC1 Huddle Board

Brainstorming, innovation, problem solving, and negotiation have all become much more productive and valuable if people can easily collaborate in real time with minimal friction.

Sarah Ieroianni

Brother QL-820NWB Professional Label Printer

The print quality also does not disappoint, it’s clear, bold, doesn’t smudge and the text is perfectly sized.

Ratchada Dunn

Sharp PN-40TC1 Huddle Board

The Huddle Board’s built in program; Sharp Touch Viewing software allows us to easily manipulate and edit our documents (jpegs and PDFs) all at the same time on the dashboard.

George Khoury

Sharp PN-40TC1 Huddle Board

The biggest perks for me would be that it comes with easy to use and comprehensive programs that make the collaboration process a whole lot more intuitive and organic

Featured Content

Product Launch Showcase

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?