Using e.printStackTrace() in Java
Asked Answered
H

5

66

Why does NetBeans IDE raise a flag on printing a Java stack trace?

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

For some reason the printStackTrace is underlined in a squiggly line. When I Quick-Fix (Alt+Enter), NetBeans reports Throwable.printStackTrace() should be removed. What does this mean?

Hypha answered 18/8, 2011 at 0:3 Comment(0)
G
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.

Grunberg answered 18/8, 2011 at 0:16 Comment(1)
using a logging framework like log4j is one such better method.Bluff
R
47

Try:

e.printStackTrace(System.out);
Ruthie answered 18/8, 2011 at 0:6 Comment(4)
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?Scarberry
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.Boesch
@Boesch System.err will print to the default error stream, not System.out. System.out represents the default output stream.Hemihedral
Would it be better to use e.printStackTrace(System.err);?Anorak
G
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.

Grunberg answered 18/8, 2011 at 0:16 Comment(1)
using a logging framework like log4j is one such better method.Bluff
U
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.

Unbidden answered 18/8, 2011 at 0:8 Comment(0)
S
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.

Samirasamisen answered 18/8, 2011 at 0:8 Comment(2)
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'.Edington
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.Weinberger
I
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:

Inexhaustible answered 6/8, 2017 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.