I’ve been programming PHP for a long time, but not so much PHP 5… I’ve known about exception handling in PHP 5 for some time, but never really looked into it. After a quick Google it seems fairly pointless to use exception handling – I can’t see the advantages of using it over just using some if() {} statements, and perhaps my own error handling class or whatever.
There’s got to be a bunch of good reasons for using it (I guess?!) otherwise it wouldn’t have been put into the language (probably). Can anyone tell me of some good benefits it has over just using a bunch of if statements or a switch statement or something?
Exceptions allow you to distinguish between different types of errors, and is also great for routing. For example…
The thrown exception can then be handled by a custom error handler.
Since PHP is a loosely typed language, you might need to ensure that only strings are passed as arguments to a class method. For example…
…or if you need to handle different types of exceptions in different ways.
If using transactions in something like Zend Framework: