If you do any serious programming, whether it's in PHP 5 or some other language, you've needed to know how to handle run time errors and other "exceptional" conditions. You can do this by making your program throw generic exceptions. Or you can unlock the potential of PHP 5 and learn how to create custom exceptions, which is the subject of this four-part series.
As you may know, many mature object-oriented programming languages, like Java and C++, let developers work with exceptions in a fairly simple fashion. Of course, in these specific cases, this feature has been available for a long time, and it’s been a generous source of inspiration for younger languages, including PHP 5.
While PHP 5's exception mechanism is pretty solid and can be mastered by inexperienced programmers in a short time, its full potential is rarely exploited when developing web applications. What’s more, if you’re anything like me, then it’s probable that in the past few years you’ve built some scripts that handled all run time errors and “exceptional” conditions by means of abrupt “die()” statements.
Yes, I have to admit that I’m guilty of that mistake too. I’m trying hard not to do it anymore, since I learned how to work with exceptions. Now, speaking more seriously, I’m sure that you already know how to wrap your PHP 5 scripts into neat “try-catch()” blocks, and of course, all of your classes handle certain types of errors by means of clean exceptions. That sounds fine.
However, let me ask you the following question: how far did you go when using PHP 5’s exception mechanism? If your answer is simply: "hey, generic exceptions are all that I need for my web applications," then this series of articles might be useful to you. In it, you’ll learn how to create subclasses from the built-in “Exception” class bundled with PHP 5, in this way providing your programs with the ability to handle customized exceptions.
Now that you’re aware of the goal of this series, it’s time show some action, so jump ahead and start learning how to use inheritance to throw and handle specific exceptions in PHP 5. It’s going to be a fun experience!