Probably a newbie question, but everyone seems to use e.printStackTrace()
, but I have always used System.out.println(e)
when exception handling. What i
The output stream used is not the same as pointed out by @Brian, but the level of detail is not the same either - you can try with the simple test below. Output:
With println
: you only know what exception has been thrown
java.lang.UnsupportedOperationException: Not yet implemented
With printStackTrace
: you also know what caused it (line numbers + call stack)
java.lang.UnsupportedOperationException: Not yet implemented
at javaapplication27.Test1.test(Test1.java:27)
at javaapplication27.Test1.main(Test1.java:19)
public static void main(String[] args){
try {
test();
} catch (UnsupportedOperationException e) {
System.out.println(e);
e.printStackTrace();
}
}
private static void test() {
throw new UnsupportedOperationException("Not yet implemented");
}
System.out.println(e)
will not give you your stack trace, just the error message and exception type, as well as being printed to the standard output as opposed to error output.
Bellow Program show the difference details
System.out.println(e); :- only showing the exception. e.g.java.lang.ArithmeticException: / by zero e.printStackTrace(); :- showing the details of exception and where to cause exception in program with line number e.g. java.lang.ArithmeticException: / by zero at test.Test.main(Test.java:7)
package test;
public class Test {
public static void main(String args[]) {
try {
System.out.println(12 / 0);
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
}
}
If you use System.out.println
, then you're dumping your errors to the stdout
, not stderr
.
It's traditional to dump errors to standard error, so you can filter normal successful output from the error output. It's a common practise for command-line utilities and consequently a good idea to follow.
e.g.
myCommand 2> /tmp/errors > /tmp/results
will write errors to one log, and the results to another. Depending on your shell/invoking process etc. you can combine this info, throw errors away, react if any errors are thrown etc. See here for more info.
Using printStackTrace()
is a good idea since you're dumping out where the exception took place. This is often invaluable for tracking errors that are unexpected since it'll give you a direct (if verbose) pointer to where exactly you ran into an error.
Since the output of e.printStackTrace();
is System.err
and usually I output my app log to a file, I recommend you to use both System.err
and System.out
to output errors.
public static void log(Exception e) {
e.printStackTrace(); // This goes to System.err
e.printStackTrace(System.out);
}
This way you can see the errors in the log file (in case you have one) and in the console.
System.out.println(e)
is equivalent to System.out.println(e.toString())
: System.out
is a PrintStream, PrintStream.println(Object o)
calls PrintStream.println(o.toString())
.
e.toString()
returns the name of the class, and the exception's getLocalizedMessage().
e.printStackTrace()
writes that information to System.err (not System.out) and also a stack trace, that is, the chain of methods that led to the exception. This is useful information.
I've often thought it would be nice if there was a method that returns a String containing the info that's output by e.printStackTrace()
. Since there isn't you have to use e.getStackTrace()
and write your own routine to output the resulting array of StackTraceElements.