code: public static <T extends Throwable> T getCause( final Throwable    ex,
                                                      final Class<T>     exceptionClass )
      {
          Throwable    cause = ex;
  
          while( cause != null && ! exceptionClass.isInstance( cause ) )
          {
              cause = cause.getCause();
          }
  
          return ( cause == null ) ? null : exceptionClass.cast( cause );
      }
docs: Checks to see if an exception or any of its causes are of a certain type. Returns the first
  type in the chain if it exists, or null if no causes are of this type.
  @param ex exception to check
  @param exceptionClass class of the exception to find
  @param <T> type of class
  @return null or an exception