When to log a stacktrace for a caught exception

I personally try to obey these rules:

  • if I can handle the exception in the catch in a ‘recoverable’ way (e.g. a DateFormatException), no need to trace the stack

  • if I want to rethrow the exception, log no stack trace. (rethrow in a chained way to retain this information)

  • if I handle the exception in a catch block as an error case (e.g. sql error), I log the stack trace.

  • if it’s a runtime exception, I would suggest the framework (yours or whatever you use) does the tracing.

Leave a Comment