views:

74

answers:

1

Hello,

Just wonder how much cost to raise a java exception (or to call native fillInStackTrace() of Throwable) compared to what it cost to log it with log4j (in a file, with production hard drive)...

Asking myself, when exceptions are raised, does it worth to often log them even if they are not necessary significant... (i work in a high load environment)

Thanks

+4  A: 

I assume from your jee6 tag that you are specifically talking about exceptions in Java.

If you have so many exceptions that logging them is a performance issue, you should probably reconsider your use of exceptions.

Exceptions should be used for exceptional situations. If you are doing something so frequently that the logging becomes a performance issue, it's probably not an exceptional situation.

To answer your specific question: logging will be several orders of magnitude slower. If you know that you won't need the exceptions logged then you should be able to improve the performance of your application by not logging them.

Mark Byers
That's not as clear cut. There are a couple of situations, where (at least in C++) exceptions are a most elegant flow control mechanism
EFraim
@Efraim: Also in Python exceptions are much more acceptable. For example breaking out of a loop is done by raising a StopIteration. But I think in this case the OP was specifically looking for a Java related answer. I'll reword my answer ot make that clear.
Mark Byers
actually i'm just someone that manage to resolve production bugs not found/reproductible in dev env on a website having 10 000 000 of unique visitors each month. I know everything is not always ok about design and exception handling but just have to deal with that, can't refactor everything. Just want to know in term of approximative ratio how much costy is to log an exception compared to raising in order to adapt logging strategies...
Sebastien Lorber
@Sebastian: OK see update.
Mark Byers
ThanksAnyway I guess my question is stupid, it's always faster to read memory than writing on a drive...
Sebastien Lorber