code: public Iterator<Throwable> iterator() {
  
         return new Iterator<Throwable>() {
  
             SQLException firstException = SQLException.this;
             SQLException nextException = firstException.getNextException();
             Throwable cause = firstException.getCause();
  
             public boolean hasNext() {
                 if(firstException != null || nextException != null || cause != null)
                     return true;
                 return false;
             }
  
             public Throwable next() {
                 Throwable throwable = null;
                 if(firstException != null){
                     throwable = firstException;
                     firstException = null;
                 }
                 else if(cause != null){
                     throwable = cause;
                     cause = cause.getCause();
                 }
                 else if(nextException != null){
                     throwable = nextException;
                     cause = nextException.getCause();
                     nextException = nextException.getNextException();
                 }
                 else
                     throw new NoSuchElementException();
                 return throwable;
             }
  
             public void remove() {
                 throw new UnsupportedOperationException();
             }
  
         };
  
      }
docs: Returns an iterator over the chained SQLExceptions.  The iterator will
  be used to iterate over each SQLException and its underlying cause
  (if any).
  
  @return an iterator over the chained SQLExceptions and causes in the proper
  order
  
  @since 1.6