PHP5 Exception handling


What are Exceptions? - The Concept

Exceptions are used to handle (unforseen) Error Conditions within your Application and offer a comfortable way of Error Handling. Languages like Java implement Exceptions in a rather strict way (checked Exceptions, Propagation), whereas in PHP5, Exceptions are used as an addon which you can use, but don't have to. To use this mechanism, try/catch-block support is provided. This means, you can execute code in a sandboxed environment and react on possible failures of the called code.

Basic Ideas

Difference to 'normal' Error Handling

While standard Error Handling must be performed, no matter if you use Exceptions or not, there's only one big difference between them: they're used for different things. No doubt that you can do all your Error Handling using Exceptions, or handle Exceptions with standard methods, but you'll get a mess - and won't profit of any of them. So it's a matter of the correct mix to use its full potential. Use Exceptions rarely and only where appropriate.

PHP Exceptions vs Java Exceptions

In Java, C# and other languages, a 'finally' statement exists to be used for processing after leaving the try/catch block. This isn't the case with PHP, where code execution simply continues once an Exception has been thrown (and handled).