Dump stack Trace To String - Java java.lang

Java examples for java.lang:Exception StackTrace

Description

Dump stack Trace To String

Demo Code

//package com.java2s;
import java.io.PrintWriter;
import java.io.StringWriter;

public class Main {
    public static String stackTraceToString(StackTraceElement[] stackTrace) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < stackTrace.length; i++) {
            StackTraceElement el = stackTrace[i];
            sb.append("    ").append(el.toString()).append("\n");
        }//from w  w  w  .  jav a2  s . c  o  m
        return sb.toString();
    }

    public static String stackTraceToString(Throwable t) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        t.printStackTrace(pw);
        return sw.toString();
    }
}

Related Tutorials