Handling Exceptions – your partner in crime
To find the source of errors, good error handling should be implemented. In this recipe we'll talk about Exception handling within the Zend Framework 2 and how to optimally use it.
Getting ready
We can safely assume that we all know about try-catch and Exceptions, but to make sure nobody is caught out please take a look at the link to the PHP manual in the See also subsection in this section.
How to do it...
Exception handling is not that difficult to use, but it is a very useful tool if used correctly.
Exception classes in Zend Framework 2
Let's take a look at the following example:
<?php // This non existing method throws a couple of Exception, which // is a PDOException, BadMethodCallException and probably more. try { $object->executeMe(); } catch(PDOException $e) { // We catch the most specific Exception first, as this is an // Exception that has to do with a database query that went wrong } catch(BadMethodCallException $e) { /...