From: "lroland@..." Date: 2005-11-10T02:17:13+09:00 Subject: 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