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

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

+86
java exception
Jul 22 '09 at 20:03
source share
3 answers

You do not need; this information is present elsewhere in the stack trace. From the printStackTrace() :

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

This transcript can significantly reduce the length of the output in the general case when the wrapped exception is selected from the same method as the “causal exception”.

In other words, "... x more" appears only in the chain exception and only when the last lines x stack trace are already present as part of another chain of monitored exception chains.

Suppose the method catches the Foo exception, terminates it in the exception panel, and throws Bar. Then the stack trace of Foo will be shortened. If for some reason you need a full trace, all you have to do is take the last line before ... in the trace of the Foo stack and look for it in the trace of the Bar stack; everything below this line is what would be printed in the Foo stack trace.

+112
Jul 22 '09 at 20:08
source share

Quickly guess the method for you.

 static void printLongerTrace(Throwable t){ for(StackTraceElement e: t.getStackTrace()) System.out.println(e); } 
+5
Jul 22 '09 at 20:08
source share

Let's take a stack trace from the Throwable.printStackTrace () documentation:

 HighLevelException: MidLevelException: LowLevelException at Junk.a(Junk.java:13) at Junk.main(Junk.java:4) Caused by: MidLevelException: LowLevelException at Junk.c(Junk.java:23) at Junk.b(Junk.java:17) at Junk.a(Junk.java:11) ... 1 more Caused by: LowLevelException at Junk.e(Junk.java:30) at Junk.d(Junk.java:27) at Junk.c(Junk.java:21) ... 3 more 

Reasons are displayed from the nest below (the “root cause”) to the one to which the stack fingerprint belongs.

In this case, the main cause is a LowLevelException , which raised a MidLevelException , which raised a HighLevelException .

To get the full stack trace, you should look at frames including exceptions (and including exceptions):

  1. See how many frames were dropped: "... X more"
  2. Look for skipped frames in inclusive exception
    1. See how many frames were dropped: "... Y more"
    2. Add the first X - Y frames to the stack trace
  3. If Y> 0, repeat step 2, indicating the number of frames skipped

Therefore, if we want to get a full trace of the LowLevelException stack LowLevelException we will do the following:

  1. See how many frames were dropped: "... 3 more"
  2. Look for skipped frames in an inclusive exception ( MidLevelException )
    1. 1 frame omitted ("... 1 more")
    2. Add first 2 (3 - 1) frames to stack trace
  3. Repeat step 2 with 1 as the number of frames skipped.
    1. Look at MidLevelException ( HighLevelException )
    2. Add first 1 frame to stack trace

Your full stack trace looks like this:

 LowLevelException at Junk.e(Junk.java:30) at Junk.d(Junk.java:27) at Junk.c(Junk.java:21) // From MidLevelException stack trace at Junk.b(Junk.java:17) at Junk.a(Junk.java:11) // From HighLevelException stack trace at Junk.main(Junk.java:4) 



Side notes:

  • There may be cases where frames are not specified, for example:

     HighLevelException: MidLevelException at Junk.main(Junk.java:4) Caused by: MidLevelException ... 1 more 

    This can happen when the reason for creating it is on the same line: new HighLevelException(new MidLevelException()) . Don’t be embarrassed by this, the approach described above is still working, there are simply no frames that can be used from the exception, continue with including it.

  • In some cases, you can save the expense by looking at the first frame that was not skipped (line above ... X more ). If you know which methods call the method on this line, you can directly look for callers in frames that include exceptions:

     HighLevelException: MidLevelException: LowLevelException at Junk.c(Junk.java:29) at Junk.b(Junk.java:21) at Junk.a(Junk.java:13) at Junk.main(Junk.java:4) Caused by: MidLevelException // You know Junk.d is only called by Junk.b at Junk.d(Junk.java:35) ... 3 more 
0
May 10 '19 at 11:25
source share



All Articles