A simple Exception cause-walker that picks up the messages from Exceptions and makes a string out of them in similar fashion as Stack Trace is printed. - Java java.lang

Java examples for java.lang:Exception StackTrace

Description

A simple Exception cause-walker that picks up the messages from Exceptions and makes a string out of them in similar fashion as Stack Trace is printed.

Demo Code


//package com.java2s;

public class Main {
    /**/*from w w w.  j  a v a 2  s.  com*/
     * A simple Exception cause-walker that picks up the messages from Exceptions and makes a string out of them in
     * similar fashion as Stack Trace is printed.
     * 
     * @param t
     *            Throwable
     * @return String
     */
    public static String toHumanReadable(Throwable t) {
        Throwable e2 = t;
        StringBuilder sb = new StringBuilder();
        sb.append(t.getMessage()).append("\n");
        while (e2.getCause() != null) {
            if (e2.getCause().getMessage() != null) {
                sb.insert(0, e2.getCause().getMessage() + "\n");
            }
            e2 = e2.getCause();
        }
        return sb.toString();
    }
}

Related Tutorials