Tomáš Hubálek Blog: Bavte se přiměřeně

Calendar

< September 2007 >
MonTueWedThuFriSatSun
12
3456789
10111213141516
17181920212223
24252627282930

Čtvrtek, 13.09.07

Java: How to display incomplete stacktrace?

A few days ago we met some issue and there was huge stacktrace finishing with ... 20 more.

We was disappointed that we cannot see real cause of the exception because we thought that Java is hiding most important exception details.

But in fact, there is nothing hidden, it is just misunderstanding of the stack trace.

Lets look at following java snippet:

01 public class Test {
02 
03    private static void a() {
04       try {
05          b();
06       catch (Exception e) {
07          throw new RuntimeException("Error occured in method a()", e);
08       }
09    }
10 
11    private static void b() {
12       try {
13          c();
14       catch (Exception e) {
15          throw new RuntimeException("Error occured in method b()", e);
16       }
17    }
18 
19    private static void c() {
20       throw new RuntimeException();
21    }
22 
23    public static void main(String a[]) throws Exception {
24      Test.a();
25    }
26 
27 }

Java2html

This code display following stack trace:

Exception in thread "main" java.lang.RuntimeException: Error occured in method a()
        at Test.a(Test.java:7)
        at Test.main(Test.java:24)
Caused by: java.lang.RuntimeException: Error occured in method b()
        at Test.b(Test.java:15)
        at Test.a(Test.java:5)
        ... 1 more
Caused by: java.lang.RuntimeException
        at Test.c(Test.java:20)
        at Test.b(Test.java:13)
        ... 2 more

and you can see complete stack trace although there is text .. 2 more and the real cause you can see at the top of the last exception.

For more info see description of the "bug" number 4775147 (Incomplete stack trace printed by Throwable.printStackTrace in JDK 1.4+) in Sun's bug parade.