Why Exceptions? - The Controversy
The exceptions concept has been discussed over and over again, some people argue that it has it's right to exist in the programming world, others think it's of no worth at all and makes everything more complicated. To me, this seems like one of the standard discussions, in the line of objectoriented vs procedural programming, c vs cxx or handcode vs case - there's place *for* everything, and there's always a bit of sense *in* everything.
The main problems seen with exceptions are:
- Overuse of Exception Handling
Some people attempt to do all their Error Handling using Exceptions. This will end in unreadable code full of trying and catching.
- Checked Exceptions vs Unchecked Exceptions
You have to handle all exceptions, otherwise PHP5 will throw an Unchecked Exception Error. You have to handle it, but you don't have to do anything with it (ie leave empty catch block, use generic exception)
- Exceptions Considered Harmfull
Like gotos, Exceptions jump from one piece of code to another place, thus making it hard to read code and keep the overview.
Exception Handling is especially usefull when working with unknown libraries, devices or operations. An Author of a library might consider throwing Exceptions whenever an important Error occurs within his code and leave their handling to the user. Exceptions give you a superior alternative to simple return statements, in that they contain much more information about the location in the code where the Exception was thrown and enable you to have more control throughout the Error Handling process.