I'm totally in agreement with Yoav -- log an exception at the place where it's caught and handled. Handled meaning not rethrown. The only exception to this would be code that's called by third-party libraries if those third-party libraries don't properly log or propagate exceptions. But libraries like that typically need to be isolated by wrapper-code anyway, and the wrapper-code whould take care of properly logging exceptions, even if it does rethrow them.
Now for the case when you have 50 methods calling MethodC and logging when they catch an exception thrown by MethodC. I think that's an indication you need to write a MethodCThatLogsButDoesntThrow, either as a convenience decorator method around the original MethodC or as a replacement for it. The stereotypical case of this is when you've got some specialized collection of objects and there's a getter that throws if you ask for an object not in the collection. In such cases, you typically want a convenience version of the getter that, say, returns null if the object is not found, and logs the error. --Ian --- Raziel Steinmetz <[EMAIL PROTECTED]> wrote: > Yoav, > > I agree with this approach. The only drawback is that you might need > to > duplicate your logging code. For example : MethodA and MethodB both > call > MethodC. MethodC finds an error and throw an exception. Then both > MethodA > and MethodB will need to log this error. And what if you have 50 > methods > calling MethodC? On the other hand if you log the error in MethodC > then you > have only one place that handles the logging for this error. > > Don't get me wrong, with your approach (which is the one that I use > in my > code) you will have only one log for each error but the code might be > harder > to maintain. > > Raz. > > -----Original Message----- > From: Shapira, Yoav [mailto:[EMAIL PROTECTED] > Sent: Wednesday, March 17, 2004 9:00 AM > To: Log4J Users List > Subject: RE: RE: when to log Exceptions > > > > Hi, > > >Exceptions are often caught and rethrown. > > If you're the end of the catching chain, i.e. if you don't rethrow > it, > log it. If you do rethrow it, no need to log because you're not > really > handling the exception. This approach is easy and will work with > external libraries as well, without having to throw everything as > your > own exception with meta info like hasBeenLogged. > > Yoav Shapira > > > > This e-mail, including any attachments, is a confidential business > communication, and may contain information that is confidential, > proprietary > and/or privileged. This e-mail is intended only for the > individual(s) to > whom it is addressed, and may not be saved, copied, printed, > disclosed or > used by anyone else. If you are not the(an) intended recipient, > please > immediately delete this e-mail from your computer system and notify > the > sender. Thank you. > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
