Android Utililty Methods Throwable to String Convert

List of utility methods to do Throwable to String Convert

Description

The list of methods to do Throwable to String Convert are organized into topic(s).

Method

Stringstringify(Throwable t)
stringify
if (t == null) {
    return null;
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
return sw.toString();
StringgetExceptionMessage(Throwable e)
get Exception Message
String message = e.getMessage();
if (StringUtil.isEmpty(message)) {
    message = e.getClass().getSimpleName()
            .replaceFirst("Exception", "");
if (message.length() > LEN) {
    message = message.substring(0, LEN) + "...";
return message;
StringgetExceptionMessage(Throwable ex)
get Exception Message
StringBuilder sb = new StringBuilder(ex.getClass().getSimpleName())
        .append(":");
if (ex.getMessage() != null) {
    sb.append(ex.getMessage()).append("\n");
for (StackTraceElement element : ex.getStackTrace()) {
    sb.append(element.toString()).append("-")
            .append(element.getLineNumber()).append("\n");
...
StringexceptionToString(Throwable t)
exception To String
if (t == null)
    return null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
    t.printStackTrace(new PrintStream(baos));
} finally {
    try {
        baos.close();
...
voidfailException(String message, Throwable got)
Call this if you have an exception you want to fail for!
AssertionError ae = new AssertionError(message);
if (isandroid.get()) {
    System.out.println("ANDROID: About to throw AssertionError "
            + "but we cannot initialize the cause... "
            + "Here follows both the AssertionError and its cause");
    ae.printStackTrace();
    System.out.println("And the cause...");
    got.printStackTrace();
...