When to log a stacktrace for a caught exception

后端 未结 5 1127
萌比男神i
萌比男神i 2020-12-02 00:51

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

5条回答
  •  难免孤独
    2020-12-02 01:26

    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.

提交回复
热议问题