Using e.printStackTrace() in Java

JavaIoexceptionPrintstacktrace

Java Problem Overview


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!

Java Solutions


Solution 1 - Java

Try:

e.printStackTrace(System.out);

Solution 2 - Java

It is just a recommendation. In eclipse it is fine - I believe it is just the IDE telling you that there are more conventional methods of doing it, like some of the other answers. I find that it is useful for debugging, and that you should tell users when a fatal error is going to occur, to use a debug mode (like a console switch -d) to collect these logs.

Solution 3 - Java

It's probably because printStackTrace() doesn't really handle the error as much as it just dumps the stack in the console. It acts as a placeholder until you replace it with proper error handling (if it is needed at all) and replace the output with a logger of some sort.

Solution 4 - Java

> e.printStackTrace();

Is not good practice because it prints in the default ErrorStream, which most of the times is the console!

NetBeans should be warning you about that. The good practice about it, is logging the message. Follow same reference:

http://onjava.com/pub/a/onjava/2003/11/19/exceptions.html

EDIT See first comment bellow to more info.

Solution 5 - Java

Just printing a stack trace is not enough. Printing the exception's stack trace in itself doesn't mean that it is completely bad practice, but printing only the stack trace when an exception occurs is an issue.

Always log exceptions(using a good logging framework), but do not expose them to the end-user. And keep ensure that showing stack traces only in development mode.

I myself use(most of the time) logger.log(Level.SEVERE, <exception>.getMessage(), <exception>);.

when netbeans suggest you to handle the exception 'Surround Statement with try-catch', if you click on this, it will generate):

try {
    //something need to be handle(exception that throw)
} catch (SQLException ex) {
    Logger.getLogger(ClassName.class.getName()).log(Level.SEVERE, null, ex);
}

Which is better than ex.printStackTrace();.

These may help:

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionO_OView Question on Stackoverflow
Solution 1 - JavaPablo FernandezView Answer on Stackoverflow
Solution 2 - JavaMgamerzView Answer on Stackoverflow
Solution 3 - Javadee-seeView Answer on Stackoverflow
Solution 4 - JavaArthur NevesView Answer on Stackoverflow
Solution 5 - JavaBlasankaView Answer on Stackoverflow