get Stack Trace and return a String - Java java.lang

Java examples for java.lang:Exception StackTrace

Description

get Stack Trace and return a String

Demo Code


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

public class Main {

  public static String getStackTrace(Throwable t) {
    String stackTrace = null;/*from  w  ww  .  j  av  a2  s  .c om*/
    try {
      StringWriter sw = new StringWriter();
      PrintWriter pw = new PrintWriter(sw);
      t.printStackTrace(pw);
      pw.close();
      sw.close();
      stackTrace = sw.getBuffer().toString();
    } catch (Exception ex) {
    }
    return stackTrace;
  }
} 

Related Tutorials