Convert Stack Trace in Throwable to String - Java java.lang

Java examples for java.lang:Exception

Description

Convert Stack Trace in Throwable to String

Demo Code

import java.io.PrintWriter;
import java.io.StringWriter;

public class Main{

    public static String getStackTrace(Throwable th) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        th.printStackTrace(pw);/*ww w.  j  a  v  a  2  s. co  m*/
        pw.close();
        return sw.toString();
    }

}

Related Tutorials