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.
I suggest that you use the ExceptionUtils class from Apache Commons lang, which provides useful method for that.
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();
}
}
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.