Returns the root cause of the specified exception. - Java Reflection

Java examples for Reflection:Annotation

Description

Returns the root cause of the specified exception.

Demo Code

/*/*from www .  j a  v a 2  s  .c  o  m*/
 * Copyright (c) 2015-2016 QuartzDesk.com.
 * Licensed under the MIT license (https://opensource.org/licenses/MIT).
 */
//package com.java2s;

import java.sql.SQLException;

public class Main {
    /**
     * Returns the root cause of the specified exception.
     *
     * @param t an exception.
     * @return the root cause.
     */
    public static Throwable getRootCause(Throwable t) {
        Throwable cause = t;

        while (getCause(cause) != null)
            cause = getCause(cause);

        return cause;
    }

    /**
     * Returns the cause of the specified exception.
     *
     * @param t an exception.
     * @return the cause.
     */
    public static Throwable getCause(Throwable t) {
        // SQLException does not use "standard" cause chaining...grrr
        if (t instanceof SQLException) {
            return ((SQLException) t).getNextException();
        } else {
            return t.getCause();
        }
    }
}

Related Tutorials