dump the exception to string - Android java.lang

Android examples for java.lang:Throwable

Description

dump the exception to string

Demo Code


//package com.java2s;

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

public class Main {
    /**/*from www.  ja  va  2s.  c  om*/
     * dump the exception to string
     */
    public static String dumpException(Throwable throwable) {
        StringWriter stringWriter = new StringWriter(160);
        stringWriter.write(throwable.getClass().getName());
        stringWriter.write(":\n");
        throwable.printStackTrace(new PrintWriter(stringWriter));
        return stringWriter.toString();
    }

    /**
     * make throwable to string.
     */
    public static String toString(Throwable throwable) {
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        printWriter.print(throwable.getClass().getName() + ": ");
        if (throwable.getMessage() != null) {
            printWriter.print(throwable.getMessage() + "\n");
        }
        printWriter.println();
        try {
            throwable.printStackTrace(printWriter);
            return stringWriter.toString();
        } finally {
            printWriter.close();
        }
    }
}

Related Tutorials