« Return to Thread: Error handling

Re: Error handling

by Michael Irey :: Rate this Message:

Reply to Author | View in Thread

Matthew Weier O'Phinney-3 wrote:
-- Ed Lazor <edlazor@internetarchitects.biz> wrote
(on Wednesday, 01 July 2009, 02:34 PM -0700):
> So where would you handle your logging if not from with a base
> exception class?  Are you including the logging as part of the "// Bla
> blah" part that you have listed below?

Your ErrorController::errorAction() is a good place for such activities.
:)

--
Matthew Weier O'Phinney
Project Lead            | matthew@zend.com
Zend Framework          | http://framework.zend.com/

I agree you should have have logging in the ErrorController::errorAction() if you want to, however what if you want to log exceptions that end up getting caught.

There are 2 options as I see it, option 1 will only log exceptions that are caught, which never ends up happening anyways because the whole zf application is essentially wrapped in a try/catch block. Option 2 however will provide logging for Exceptions regardless of whether they are caught or not.

Any feedback on these 2 options would be appreciated.

Option 1:

function exception_handler($exception) {
  // Do logging stuff here
}

set_exception_handler('exception_handler');

throw new Exception('Uncaught Exception');


-- or --

Option 2:

class BaseException extends Exception
{
    public function __construct($message, $code = 0) {
        // Do logging stuff here
   
        parent::__construct($message, $code);
    }

}

class MyCustomException extends BaseException {}


try {
    // if something happened
    throw new MyCustomException('This Exception get caught');
}
catch (Exception $e) {
    // handle the exception here, and this will automatically get logged.
}

 « Return to Thread: Error handling