is Exception Caused By - Java java.lang

Java examples for java.lang:Throwable

Description

is Exception Caused By

Demo Code


import java.io.PrintWriter;
import java.io.StringWriter;
import javax.servlet.http.HttpServletRequest;

public class Main{
    //from  w ww . j  av  a  2s .c  om
    @SafeVarargs
    public static boolean isCausedBy(Exception ex,
            Class<? extends Exception>... causeExceptionClasses) {
        Throwable cause = ex.getCause();
        while (cause != null) {
            for (Class<? extends Exception> causeClass : causeExceptionClasses) {
                if (causeClass.isInstance(cause)) {
                    return true;
                }
            }
            cause = cause.getCause();
        }
        return false;
    }
}

Related Tutorials