12

Java's e.printStackTrace() doesn't print all the details of the inner exception's stack trace.

Is there a ready way to generate the complete stack trace in string form? (besides formatting it myself)

Edit

I just found out what printStackTrace() does - apparently the stack frames it filters out are exactly the ones common to the inner exception and the outer one. So in fact it is rather what I want, and not the 'full' stack trace.

ripper234
  • 222,824
  • 274
  • 634
  • 905
  • How do you mean - inner exception's stack trace? Is a caught exception being propagated in another exception? – Everyone Aug 18 '09 at 10:10
  • Something that's produces by a catch ... throw new Exception("foo", e) – ripper234 Aug 18 '09 at 11:27
  • I just found out what printStackTrace() does - apparently the stack frames it filters out are exactly the ones common to the inner exception and the outer one. So in fact it is rather what I want, and not the 'full' stack trace. – ripper234 Aug 18 '09 at 11:52

3 Answers3

9

I suggest that you use the ExceptionUtils class from Apache Commons lang, which provides useful method for that.

Romain Linsolas
  • 79,475
  • 49
  • 202
  • 273
8

I ended up rolling my own (I took the implementation of Throwable.printStackTrace() and tweaked it a bit):

public static String joinStackTrace(Throwable e) {
    StringWriter writer = null;
    try {
        writer = new StringWriter();
        joinStackTrace(e, writer);
        return writer.toString();
    }
    finally {
        if (writer != null)
            try {
                writer.close();
            } catch (IOException e1) {
                // ignore
            }
    }
}

public static void joinStackTrace(Throwable e, StringWriter writer) {
    PrintWriter printer = null;
    try {
        printer = new PrintWriter(writer);

        while (e != null) {

            printer.println(e);
            StackTraceElement[] trace = e.getStackTrace();
            for (int i = 0; i < trace.length; i++)
                printer.println("\tat " + trace[i]);

            e = e.getCause();
            if (e != null)
                printer.println("Caused by:\r\n");
        }
    }
    finally {
        if (printer != null)
            printer.close();
    }
}
ripper234
  • 222,824
  • 274
  • 634
  • 905
  • have you missed to print stack trace for inner exception here? `if (e != null) printer.println("Caused by:\r\n");` – VSB Apr 22 '19 at 05:34
  • something like `printer.println("Caused by:\r\n"+joinStackTrace(e))`? – VSB Apr 22 '19 at 05:35
2

Yes you can use the StackTraceElement class returned by Throwable.getStackTrace() and find the details.

From the API:

The last element of the array (assuming the array's length is non-zero) represents the bottom of the stack, which is the first method invocation in the sequence.

techzen
  • 2,895
  • 2
  • 22
  • 22