Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to log a stacktrace for a caught exception

I recently asked whether to report the getMessage() text of a caught exception. Rather surprisingly, most answers misunderstood my question and thought I was asking whether to report the stack-trace for a caught exception, suggesting that doing so is considered the norm. So I'm asking a follow up question.

In which circumstances should you, or should you not, report a stack-trace when you catch an exception? By "reporting" I include asking a logging framework to log the stack-trace for you.

I am not asking whether to report something. I'm asking whether that report should include the stack-trace.

like image 770
Raedwald Avatar asked Sep 09 '11 11:09

Raedwald


1 Answers

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.

like image 148
alvi Avatar answered Oct 03 '22 17:10

alvi