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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With