Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...

When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Handling Exceptions

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 TRUE value 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:

    try {
    } 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 TRUE value 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() and Zend_Controller_Response_Abstract::isException().

    By passing a boolean TRUE to Zend_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 its isException() method, and retrieving the exceptions via the getException() method. As an example:

    $response $front->dispatch();
    if (
    $response->isException()) {
    $exceptions $response->getException();
    // handle exceptions ...
    } else {

    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.

Zend Framework