Android examples for java.lang:Throwable
get Single Error Message
import java.io.PrintWriter; import java.io.StringWriter; public class Main { public static String getSingleErrorMessage(Throwable err) { boolean prependClassName = !(err instanceof java.sql.SQLException || err .getClass() == java.lang.Exception.class); return getErrorMessage(err, prependClassName); }// w ww .jav a 2s. c o m public static String getErrorMessage(Throwable err) { if (err instanceof java.sql.SQLException) { return getStackTraceErrorMessage(err); } if (err instanceof java.lang.NullPointerException) { return getStackTraceErrorMessage(err); } if (err instanceof java.io.EOFException) { return getStackTraceErrorMessage(err); } boolean prependClassName = !(err instanceof java.sql.SQLException || err .getClass() == java.lang.Exception.class); return getErrorMessage(err, prependClassName); } public static String getErrorMessage(Throwable err, boolean prependClassName) { String errMsg = err.getMessage(); if (errMsg != null) { return errMsg; } else { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); if (err instanceof java.lang.NullPointerException) { getErrorMessageByStackTrace(pw, err); } else { getSignErrorMessageByStackTrace(pw, err); } return sw.toString(); } } public static String getStackTraceErrorMessage(Throwable err) { boolean prependClassName = !(err instanceof java.sql.SQLException || err .getClass() == java.lang.Exception.class); return getStackTraceErrorMessage(err, prependClassName); } public static String getStackTraceErrorMessage(Throwable err, boolean prependClassName) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); err.printStackTrace(pw); return sw.toString(); } public static void getErrorMessageByStackTrace(PrintWriter pw, Throwable err) { err.printStackTrace(pw); } public static void getSignErrorMessageByStackTrace(PrintWriter pw, Throwable err) { Throwable ourCause = err.getCause(); if (ourCause != null) { String errMsg = ourCause.getMessage(); pw.print(errMsg); pw.print("\n"); getErrorMessageByStackTrace(pw, ourCause); } } }