Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I increase the number of displayed lines of a Java stack trace dump?

Tags:

java

exception

Is there a way to make Throwable.printStackTrace(PrintStream s) print the full stack trace, so that I can see beyond the final line of "... 40 more"?

like image 367
andersonbd1 Avatar asked Jul 22 '09 20:07

andersonbd1


People also ask

How do I print a full stack trace?

Using printStackTrace() method − It print the name of the exception, description and complete stack trace including the line where exception occurred. Using toString() method − It prints the name and description of the exception. Using getMessage() method − Mostly used. It prints the description of the exception.

How does stack trace work in Java?

Simply put, a stack trace is a representation of a call stack at a certain point in time, with each element representing a method invocation. The stack trace contains all invocations from the start of a thread until the point it's generated. This is usually a position at which an exception takes place.

Should we print stack trace?

As someone who has to troubleshoot regularly, I would never omit printing a stacktrace when something has gone wrong. Yes, don't show it to the user, but yes, dump it to an error log.

How do I print a string stack trace?

Example: Convert stack trace to a stringIn the catch block, we use StringWriter and PrintWriter to print any given output to a string. We then print the stack trace using printStackTrace() method of the exception and write it in the writer. Then, we simply convert it to string using toString() method.


2 Answers

You don't need to; that information is present elsewhere in the stack trace. From the docs of printStackTrace():

Note the presence of lines containing the characters "...". These lines indicate that the remainder of the stack trace for this exception matches the indicated number of frames from the bottom of the stack trace of the exception that was caused by this exception (the "enclosing" exception).

This shorthand can greatly reduce the length of the output in the common case where a wrapped exception is thrown from same method as the "causative exception" is caught.

In other words, the "... x more" only appears on a chained exception, and only when the last x lines of the stack trace are already present as part of another chained exception's stack trace.

Suppose that a method catches exception Foo, wraps it in exception Bar, and throws Bar. Then Foo's stack trace will be shortened. If you for some reason want the full trace, all you need to do is take the last line before the ... in Foo's stack trace and look for it in the Bar's stack trace; everything below that line is exactly what would have been printed in Foo's stack trace.

like image 144
Michael Myers Avatar answered Oct 18 '22 07:10

Michael Myers


Quick guess at a method for you.

static void printLongerTrace(Throwable t){     for(StackTraceElement e: t.getStackTrace())         System.out.println(e); } 
like image 28
jjnguy Avatar answered Oct 18 '22 09:10

jjnguy