Convert exception To String via ByteArrayOutputStream - Java java.lang

Java examples for java.lang:Exception StackTrace

Description

Convert exception To String via ByteArrayOutputStream

Demo Code


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

public class Main {

    public static String exceptionToString(Throwable t) {
        if (t == null)
            return null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {/*from  w  ww  . j  a  va2s .  c  om*/
            t.printStackTrace(new PrintStream(baos));
        } finally {
            try {
                baos.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return baos.toString();
    }
}

Related Tutorials