• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception block: e.printStackTrace()  RSS feed

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

While catching exceptions is it advisable to print the stack trace by
e.printStackTrace(); or log the error message by e.getMessage()

try{
//Exception Code
}catch(NumberFormatException ne){
e.printStackTrace();
}

I feel that if I mention e. then the log file size is getting bigger.
which is advisable?

regards
bala

 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you understand why printing the stack trace is a good idea?

It's because the stack trace contains valuable information for developers who have to debug the program. The stack trace makes it often easy to find the exact spot in the source code where a bug occurs.

If you're getting many exceptions that make your log file grow fast, then there is probably something wrong with your program. Exceptions should really only be used for error conditions, not for normal program control flow.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both can be useful. In the case of a NumberFormatException the most appropriate response might be to ask for the number to be repeated, in which case logging might not be necessary; in the case of a NullPointerException you need the stack trace to help find out why it happened and sort it out.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!