class ExceptionUtils {

    @Nullable
    public static Throwable findCause( @Nonnull Throwable exception, @Nonnull Class<?>... causeClasses )
    {
        final Set<Throwable> checkedCauses = new HashSet<>();
        Throwable cause = exception;
        do
        {
            if ( isInstance( cause, causeClasses ) )
            {
                break;
            }
            checkedCauses.add( cause );
            cause = cause.getCause();
        }
        while ( (cause != null) && !checkedCauses.contains( cause ) );
        return cause;
    }

    private  ExceptionUtils();

}