Search code examples
javaexceptionjava-ee-6high-load

Cost of creating exception compared to cost of logging it


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


Solution

  • 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.