0

I have a project which is developed in Zend Framework 1. The project is completed.

Now while I am testing the whole site, some pages thrown exceptions. One of such is below:

exception 'Zend_Paginator_Exception' with message 'No adapter for type NULL'

I have searched in net and got the steps to add try-catch to this. But it will take much time to check all code and repeat this step.

Can I write a common exception handler which catches all exceptions and handle it ?

2 Answers 2

1

Zend framework automatically handles exceptions using the errorController. You can enable it by adding the following line in you config file.

resources.frontController.throwExceptions = 0

If you want to handle exceptions manually then, rather then writing try/catch block all over the code you can centralize it using the code below.

Tell Zend Framework to not handle exceptions. Do this in your application.ini

resources.frontController.throwExceptions = 1

Define a custom method to handle exceptions in your Bootstrap class.

public function __handleExceptions(Exception $e){
        //render a view with a simple error message for the user
        //and send an email with the error to admin
    }

Override the _bootstrap() and run() methods of Zend_Application_Bootstrap_Bootstrap in your Bootstrap class as shown below. This will catch all the exceptions thrown during the bootstrapping and request dispatching process and call your custom exception handler.

protected function _bootstrap($resource = null)
    {
        try {
            parent::_bootstrap($resource);
        } catch(Exception $e) {
            $this->__handleExecptions($e);
        }
    }

    public function run()
    {
        try {
            parent::run();
        } catch(Exception $e) {
            $this->__handleExecptions($e);
        }
    }

This will eliminate the need of writing try/catch block at multiple places. Hope this helps.

Sign up to request clarification or add additional context in comments.

5 Comments

@Web_Developer What are you implying?
@Web_Developer You need to override the Bootstrap() and run() methods. In your Bootstrap class. See the details in my answer.
@Web_Developer Use the Zend_View component and render the message you get by calling $e->getMessage();
Spelling mistake when you call the function __handleExecptions Now I could reach there in the function during exception.
resources.frontController.throwExceptions = 0 It is not working.Please remove this step and modify your comment including the spelling correction..
0

If you have this line in your index.php

$application->bootstrap()->run();

You can wrap it with try catch block

try {
    $application->bootstrap()->run();
} catch (Exception $e) {
    //handle all exceptions here
}

Of course you can also have many catch blocks for different types of exceptions.

1 Comment

I have these lines and added the following: try { $application->bootstrap(); $application->run(); } catch (Exception $e) { //handle all exceptions here echo "Some error"; exit; } But still it shows Exceptions on Screen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.