Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
606 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...