make throwable to string. - Android java.lang

Android examples for java.lang:Throwable

Description

make throwable to string.

Demo Code


//package com.java2s;

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

public class Main {
    /**// www.  jav a 2  s  . co  m
     * 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