From: Robert Klemme Date: 2005-11-10T05:27:13+09:00 Subject: Re: Logging vs. Exceptions lroland@gmail.com wrote: > 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 > ----------------- It's difficult to discuss this with such an abstract example. Depending on the nature of method x and its place in the overall application architecture it may be wise or unwise to catch and log the exception... > 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 ? I'm not sure how exactly you want to do this. IMHO it's like this: the place that throws the exception never logs anything because it doesn't know how the exception is handled. On the higher level (can be arbitrary many levels in between) an exception is caught and dealt with. Dealing can mean anything from logging, over retrying to recovering and doing something else. > 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 ?. Exceptions are for exceptional cases, i.e. when a piece of code cannot continue as planned (disk full during IO for example). We had a discussion yesterday or the day before that revolved around using return values vs. exceptions. If you provide more insight into your app we might be able to come up with more specific input. Kind regards robert