get Stack Trace String - Java java.lang

Java examples for java.lang:Exception

Description

get Stack Trace String

Demo Code


//package com.java2s;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class Main {
    public static String getStackTrace2String(Exception e) {

        ByteArrayOutputStream out = null;
        PrintStream ps = null;/*from   www  .j  ava  2  s  .  c  o  m*/
        String stackTrace = "";

        try {

            out = new ByteArrayOutputStream();
            ps = new PrintStream(out);
            e.printStackTrace(ps);
            stackTrace = out.toString();

        } catch (Exception x) {

            stackTrace = "";

        } finally {

            if (null != ps)
                try {
                    ps.close();
                } catch (Exception x) {
                }
            if (null != out)
                try {
                    out.close();
                } catch (Exception x) {
                }

        }

        return stackTrace;

    }
}

Related Tutorials