Logging vs. Exceptions



Hi all

I am moving a rather big Perl application to Ruby, due to the nasty way
Perl (un)handles exceptions we originally used log4perl (a log4j/log4r
like logging facility) heavily in a 'log and recover' like manner.

Ruby however have a quite nice exception semantics and thus I am
playing with using exceptions where I used to use logging. This brings
us to the dilemma of when to throw errors and when to log them. I read
on the net that a good policy is to either 'catch, wrap and throw', or
'log and recover', but never neither nor both.

Although this sounds like a reasonable plan I contiguously find myself
in limbo between doing the one or the other or mixing both.

i.e. if I have a class which contains a public method: 'x', and a
private method: 'y' (called by 'x'). Let's also put in as constraint
that it is vital that calling method: 'x' does not blow up the program
- the solution I ended up with is to catch the possible exceptions that
'y' may have thrown, log the errors and do something to recover (i.e.
thus letting the caller of 'x' handle whatever went wrong in 'x' by the
means of status codes).

-----------------
def x
:
begin
res = y( args )
rescue
log("error calling method: y")
# the method calling x should handle this
# or maybe we should propagate the exception thrown from y
# (or re throw a new one) for the caller to handle
return nil
end

return res
end

def y
:
# do a lots of commands that may throws exceptions
if error
raise Exception.new("something went wrong")
end
:
return res
end
-----------------

Although I normally follow the guidelines of not doing both I am
leaning towards letting the exception code also use the logging
facility that way I can propagate nested exceptions up through the code
and still have the all the errors logged into one place - would this be
ugly ? or bad design ?. I am a old school Haskell, C and Perl
programmer so I am not used to do much exception handling thus I would
like some opinions on the best practices of logging vs. exception
handling ?.



Regards.

Lars Roland

.



Relevant Pages

  • Re: Logging vs. Exceptions
    ... I am moving a rather big Perl application to Ruby, ... Ruby however have a quite nice exception semantics and thus I am ... playing with using exceptions where I used to use logging. ... and throw', or 'log and recover', but never neither nor both. ...
    (comp.lang.ruby)
  • Re: Problem with KITL?
    ... > Enable kernel debugger ... This OK if KdStub stumbling on its own BP. ... Exception in debugger, Addr=0x801AB864 - attempting to recover ...
    (microsoft.public.windowsce.platbuilder)
  • Re: Using form controls from other classes.
    ... button on Form when C catches the exception. ... handler because I add the handler after creating the objects. ... If an exception is thrown and caught during initialization (whether part of construction or separate), and the fact of that exception is important to the clients of class C, why is that fact not exposed via a property? ... My Form contains the handlers for all events including logging. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Yet again - closing a thread
    ... effort was involved in adding exception handlers to recover at each level where recovery ... I was able to recover and keep running. ... it is very likely a bug in the program. ... then the Wait will not fail. ...
    (microsoft.public.vc.mfc)
  • RE: .NET Runtime 2.0 Error - I need more details...
    ... I just wanted to clarify one thing, the event logging that I'm talking ... For console appications however, the clr will ... so if you can not use this event to handle the exception. ... ConsoleException.Program.Main), calling mscorwks!JIT_Throw ...
    (microsoft.public.dotnet.framework)