Convert exception To String via ByteArrayOutputStream - Android java.io

Android examples for java.io:ByteArrayOutputStream

Description

Convert exception To String via ByteArrayOutputStream

Demo Code

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 {/*w  ww .j a v a2  s .  c om*/
            t.printStackTrace(new PrintStream(baos));
        } finally {
            try {
                baos.close();
            } catch (Exception ex) {
                ex.printStackTrace();
                System.gc();
            }
        }
        return baos.toString();
    }

}

Related Tutorials