Extract the deepest Throwable and return its message. - Java java.lang

Java examples for java.lang:Throwable

Description

Extract the deepest Throwable and return its message.

Demo Code


//package com.java2s;

public class Main {
    /**/*from   w w w .ja  v  a2 s.c o  m*/
     * Extract the deepest Throwable and return its message.
     *
     * @param ex the exception to parse the stack trace.
     * @return the simple class name and the message of the deepest throwable.
     */
    public static String extractDeepestMessage(Throwable ex) {
        if (ex == null)
            return "";
        if (ex.getCause() == null)
            return ex.getClass().getSimpleName() + ": "
                    + ex.getLocalizedMessage();
        return extractDeepestMessage(ex.getCause());
    }
}

Related Tutorials