get Exception Message - Android java.lang

Android examples for java.lang:Throwable

Description

get Exception Message

Demo Code

import android.text.TextUtils;

public class Main{

    public static String getExceptionMessage(Throwable ex) {
        StringBuilder sb = new StringBuilder(ex.getClass().getSimpleName())
                .append(":");
        if (ex.getMessage() != null) {
            sb.append(ex.getMessage()).append("\n");
        }/* www . j av a2 s  .  c  o  m*/

        for (StackTraceElement element : ex.getStackTrace()) {
            sb.append(element.toString()).append("-")
                    .append(element.getLineNumber()).append("\n");
        }

        if (ex.getCause() != null) {
            String inner = getExceptionMessage(ex.getCause());
            sb.append(inner);
        }

        sb.append("\n");
        return sb.toString();
    }

}

Related Tutorials