65

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!

O_O
  • 4,397
  • 18
  • 54
  • 69

5 Answers5

47

Try:

e.printStackTrace(System.out);
Pablo Fernandez
  • 103,170
  • 56
  • 192
  • 232
  • Ah, thanks @Pablo! Crazy, why does this make the warning disappear exactly? If you don't specify `System.out` as a parameter and leave it blank, does the JVM assume its' a debug-mode command and only outputs to the console? – Ian Campbell Sep 23 '13 at 21:52
  • 8
    Hack! This doesn't really resolve the problem as much as it just stops Netbeans showing it. This will still output to the default error stream. – ThePerson Jul 09 '14 at 08:03
  • 1
    @ThePerson `System.err` will print to the default error stream, not `System.out`. `System.out` represents the default output stream. – cst1992 Sep 05 '16 at 08:37
  • 1
    Would it be better to use `e.printStackTrace(System.err);`? – Aaron Franke Feb 24 '19 at 20:18
21

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.

Mgamerz
  • 2,872
  • 2
  • 27
  • 48
14

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.

dee-see
  • 23,668
  • 5
  • 58
  • 91
12

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.

Arthur Neves
  • 11,840
  • 8
  • 60
  • 73
  • 6
    No, it prints to the default Errorstream, which is a seperate stream, alias FileDescriptor, and can be piped to a separate file: `java YourClass 1>file.out 2>file.err` - the e.printStackTrace prints to 'file.err', not 'file.out'. – user unknown Aug 18 '11 at 01:09
  • 2
    Not always is a good practice. This console might be a console in a web server where you can't access. Sometimes it's better to have this stack trace dumped also in the current log of the application, specially in web applications. – Raul Luna Feb 10 '14 at 12:54
4

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:

Blasanka
  • 21,001
  • 12
  • 102
  • 104