Convert stack trace to string Java example shows how to convert exception stack trace to string in Java using the StringWriter, PrintWriter classes, and Apache commons library.
The stack trace is useful to debug the code and see from where exactly the exception was originated and the root cause of the problem. Stack trace of the exception can be printed using the printStackTrace
method. Consider the below-given code example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.javacodeexamples.basic.conversion; public class ConvertStackTraceToStringExample { public static void main(String[] args) { try{ method1(); }catch(Exception e){ //print stack trace e.printStackTrace(); } } private static void method1() throws Exception{ method2(); } private static void method2() throws Exception{ method3(); } private static void method3() throws Exception{ throw new Exception("Throwing Exception from method3"); } } |
Output
1 2 3 4 5 |
java.lang.Exception: Throwing Exception from method3 at com.javacodeexamples.basic.conversion.ConvertStackTraceToStringExample.method3(ConvertStackTraceToStringExample.java:24) at com.javacodeexamples.basic.conversion.ConvertStackTraceToStringExample.method2(ConvertStackTraceToStringExample.java:20) at com.javacodeexamples.basic.conversion.ConvertStackTraceToStringExample.method1(ConvertStackTraceToStringExample.java:16) at com.javacodeexamples.basic.conversion.ConvertStackTraceToStringExample.main(ConvertStackTraceToStringExample.java:8) |
By looking at the stack trace, we can know that the exception was originated from the method3
which then propagated to the method2
and method1
and finally caught in the main method where we printed the exception stack trace using the printStackTrace
method in the catch block.
Sometimes instead of printing the stack trace, we want to convert the stack trace to the string for storing it or for writing it to the log file.
How to convert stack trace to String in Java?
The stack trace can be converted to a string with the help of the StringWriter and PrintWriter classes using the printStackTrace
method of the Throwable class.
1 |
public void printStackTrace(PrintWriter pw) |
The printStackTrace
method prints the stack trace to the PrintWriter object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
package com.javacodeexamples.basic.conversion; import java.io.PrintWriter; import java.io.StringWriter; public class ConvertStackTraceToStringExample { public static void main(String[] args) { try{ method1(); }catch(Exception e){ //convert stack trace to String String strException = convertToString(e); //print stack trace from string System.out.println(strException); } } private static void method1() throws Exception{ method2(); } private static void method2() throws Exception{ method3(); } private static void method3() throws Exception{ throw new Exception("Throwing Exception from method3"); } private static String convertToString(Throwable t){ String strException = ""; //create new StringWriter StringWriter stringWriter = new StringWriter(); //create new PrintWriter on top of StringWriter PrintWriter printWriter = new PrintWriter(stringWriter); //print stack trace to StringWriter t.printStackTrace(printWriter); //convert stack trace to String strException = stringWriter.toString(); return strException; } } |
Output
1 2 3 4 5 |
java.lang.Exception: Throwing Exception from method3 at com.javacodeexamples.basic.conversion.ConvertStackTraceToStringExample.method3(ConvertStackTraceToStringExample.java:27) at com.javacodeexamples.basic.conversion.ConvertStackTraceToStringExample.method2(ConvertStackTraceToStringExample.java:23) at com.javacodeexamples.basic.conversion.ConvertStackTraceToStringExample.method1(ConvertStackTraceToStringExample.java:19) at com.javacodeexamples.basic.conversion.ConvertStackTraceToStringExample.main(ConvertStackTraceToStringExample.java:11) |
Using Apache Commons
If you are using the Apache Commons library, you can use the getStackTrace
method of the ExceptionUtils class to convert the stack trace to string.
1 |
public static String getStackTrace(Throwable t) |
This method gets the stack trace from the Throwable object as a string as given below.
1 2 |
String strException = ExceptionUtils.getStackTrace(e); System.out.println(strException); |
This example is a part of the Java String tutorial.
Please let me know your views in the comments section below.