You can use the getStackTrace () method to get an array of StackTraceElements and generate a String. Otherwise, if only the final error message is enough, use the getMessage() method suggested by Makoto.
To get the stack trace as a String from an array of StackTraceElement objects, you need to iterate over the array (taken from the JDK7 source):
StringBuilder builder = new StringBuilder(); StackTraceElement[] trace = getOurStackTrace(); for (StackTraceElement traceElement : trace) builder.append("\tat " + traceElement + "\n");
Another option is to use printStackTrace(PrintStream s) , where you can specify where you want to print the stack:
ByteArrayOutputStream out1 = new ByteArrayOutputStream(); PrintStream out2 = new PrintStream(out1); ex.printStackTrace(out2); String message = out1.toString("UTF8");
source share