Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using e.printStackTrace() in Java

This is probably a newbie question, but hope you can help me. :) I have something like this:

try {  //try to do something there } catch (IOException e) { //handle the exception  e.printStackTrace(); } 

I am using NetBeans IDE and for some reason the printStackTrace is underlined in a squiggly line. When I press Alt+Enter, it says Throwable.printStackTrace() should be removed. What does this mean? Could anyone give more insight as what this may mean? Or can I ignore this?

Thanks!

like image 723
O_O Avatar asked Aug 18 '11 00:08

O_O


People also ask

Why we should not use e printStackTrace ()?

e. printStackTrace() is generally discouraged because it just prints out the stack trace to standard error. Because of this you can't really control where this output goes. The better thing to do is to use a logging framework (logback, slf4j, java.

What is the output when we try to print exception using printStackTrace ()?

printStackTrace() The first line of output shows the same string which was returned by the toString() method for this object means Exception class name and later lines represent data previously recorded by the method fillInStackTrace().

What can I use instead of E printStackTrace?

Loggers should be used instead of printing the whole stack trace on stream. e. printStackTrace() prints a Throwable and its stack trace to stream which could inadvertently expose sensitive information. Loggers should be used instead to print Throwables, as they have many advantages.


1 Answers

Try:

e.printStackTrace(System.out); 
like image 149
Pablo Fernandez Avatar answered Sep 21 '22 16:09

Pablo Fernandez