code: public static <T extends Throwable> T findException(Class<T> exceptionTypeToFind, Throwable t) {
  
      Throwable cause = t;
  
      while (cause != null)
      {
        if( exceptionTypeToFind.isAssignableFrom( cause.getClass() ) )
        {
          //noinspection unchecked
          return (T)cause;
        }
        if( cause == cause.getCause() )
        {
          return null;
        }
        cause = cause.getCause();
      }
  
      return null;
    }
docs: Given an Exception and an Exception type to look for, finds the exception of that type
  or returns null if none of that type exist.