HomePHP Building a Modular Exception Class in PHP 5
Building a Modular Exception Class in PHP 5
Welcome to the final part of the four-part series “Subclassing exceptions in PHP 5.” By means of a hands-on approach, this series walks you through using inheritance to build finely-tuned exception subclasses. These subclasses can be used to handle, via multiple “try-catch” blocks, several failures that might occur during the execution of a given web application.
Working with generic exceptions in PHP 5 is a fairly easy process that can be tackled with minor hassles. This is true even if you're starting to get your hands dirty developing web applications that utilize them to handle a certain number of critical conditions.
Things can be a bit more complicated, however, when it's necessary to work with customized exceptions. This specific situation requires that you derive some exception subclasses from the built-in one provided by PHP 5, and that you use multiple "try-catch" blocks within the same application.
Of course, if you're an experienced PHP 5 developer with a solid background in working with exceptions, then you've probably mastered them already. However, if you're just getting started with employing them in your own applications, and want to know how to build a customized exception mechanism in a few simple steps, then this article series might be what you need.
Now, getting straight to the point, you'll possibly recall that in the last article I demonstrated how to build a customized exception system in PHP 5, which had the capacity to catch not only generic exceptions, but a couple of additional ones, triggered when working specifically with MySQL.
In short, the system in question was smart enough to handle two well-differentiated kinds of exceptions: first, the ones triggered when connecting to the MySQL server, selecting a particular database, and running SQL queries; and second, exceptions that could be thrown when manipulating result sets.
Nonetheless, handling the aforementioned types of exceptions demanded the use of two different subclasses, which in a case like this is really unnecessary. Therefore, in this concluding article of the series, I'm going to implement a similar MySQL exception mechanism, but this time by using only one subclass.
So, are you ready to tackle the final chapter of this educational journey? Let's begin now!