What is the easiest way to convert the result of Throwable.getStackTrace()
to a string that depicts the stacktrace?
The following code allows you to get the entire stackTrace with a String
format, without using APIs like log4J or even java.util.Logger
:
catch (Exception e) {
StackTraceElement[] stack = e.getStackTrace();
String exception = "";
for (StackTraceElement s : stack) {
exception = exception + s.toString() + "\n\t\t";
}
System.out.println(exception);
// then you can send the exception string to a external file.
}