Android examples for java.lang:Throwable
get Error Message from Throwable
import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.math.BigDecimal; import java.math.BigInteger; import java.net.MalformedURLException; import java.net.URL; import java.text.ParseException; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.StringTokenizer; public class Main{ public static String getErrorMessage(Throwable err) { if (err instanceof java.sql.SQLException) { return getStackTraceErrorMessage(err); }// ww w . j av a2 s . com 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(); if (!textUnit.StringIsEmpty(errMsg)) { pw.print(errMsg); pw.print("\n"); return; } getErrorMessageByStackTrace(pw, ourCause); } /* * else { StackTraceElement[] stackTrace = err.getStackTrace(); if * (stackTrace != null) getErrorStackTraceMessage(stackTrace,pw,12); } */ } }