Several mechanisms are built in to the MVC components already to allow you to handle exceptions.
-
By default, the error handler plugin is registered and active. This plugin was designed to handle:
Errors due to missing controllers or actions
Errors occurring within action controllers
It operates as a
postDispatch()plugin, and checks to see if a dispatcher, action controller, or other exception has occurred. If so, it forwards to an error handler controller.This handler will cover most exceptional situations, and handle missing controllers and actions gracefully.
-
Zend_Controller_Front::throwExceptions()By passing a boolean
TRUEvalue to this method, you can tell the front controller that instead of aggregating exceptions in the response object or using the error handler plugin, you'd rather handle them yourself. As an example:<?php
$front->throwExceptions(true);
try {
$front->dispatch();
} catch (Exception $e) {
// handle exceptions yourself
}This method is probably the easiest way to add custom exception handling covering the full range of possible exceptions to your front controller application.
-
Zend_Controller_Response_Abstract::renderExceptions()By passing a boolean
TRUEvalue to this method, you tell the response object that it should render an exception message and backtrace when rendering itself. In this scenario, any exception raised by your application will be displayed. This is only recommended for non-production environments. -
Zend_Controller_Front::returnResponse()andZend_Controller_Response_Abstract::isException().By passing a boolean
TRUEtoZend_Controller_Front::returnResponse(),Zend_Controller_Front::dispatch()will not render the response, but instead return it. Once you have the response, you may then test to see if any exceptions were trapped using itsisException()method, and retrieving the exceptions via thegetException()method. As an example:<?php
$front->returnResponse(true);
$response = $front->dispatch();
if ($response->isException()) {
$exceptions = $response->getException();
// handle exceptions ...
} else {
$response->sendHeaders();
$response->outputBody();
}The primary advantage this method offers over
Zend_Controller_Front::throwExceptions()is to allow you to conditionally render the response after handling the exception. This will catch any exception in the controller chain, unlike the error handler plugin.




