Results 1 to 5 of 5
  1. #1
    hirop35 is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default throwing exceptions in Iterator subclasses

    The java Iterator class is defined as follows:

    Java Code:
    public interface Iterator<E> {
    
            boolean hasNext();
    
        /**
         * Returns the next element in the iteration.
         *
         * @return the next element in the iteration.
         * @exception NoSuchElementException iteration has no more elements.
         */
        E next();
    
            void remove();
    }
    the comment for E next() states that subclasses will throw NoSuchElementException if the collection has no more elements. How can subclasses throw this exception when E next() has no throws declaration? My understanding of Java exception handling reuires a throws declaration on next(). Please help me to understand.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    The NoSuchElementException API docs describe its parentage as follows:

    Java Code:
    java.lang.Object
      extended by java.lang.Throwable
          extended by java.lang.Exception
              extended by java.lang.RuntimeException
                  extended by java.util.NoSuchElementException
    Runtime exceptions don't have to be declared like other sorts of exception. Another runtime exception is the NullPointerException which we all know (to our cost) can be thrown in all sorts of methods regardless of their throws declaration.

    Sun's Tutorial discusses the so-called unchecked exceptions in the section Unchecked Exceptions - The Controversy. The advice given there is "If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception."

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Also - and perhaps better - you may want to check out what the tutorial has to say about The Catch or Specify Requirement.

  4. #4
    hirop35 is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    THanks for the info. The tutorial is very informative.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    You're welcome. And welcome to the Forums! If anything there provokes a specific question (or observation), then post away. (or if my brief reference to RuntimeException wasn't enough of an answer to your original question)

Similar Threads

  1. Replies: 4
    Last Post: 08-30-2009, 12:22 PM
  2. Generics and subclasses
    By Sven in forum Advanced Java
    Replies: 1
    Last Post: 12-22-2008, 07:06 PM
  3. Main class and subclasses
    By rosh72851 in forum New To Java
    Replies: 5
    Last Post: 11-19-2008, 07:16 PM
  4. help needed with methods in subclasses
    By uncopywritable in forum New To Java
    Replies: 4
    Last Post: 08-01-2007, 02:47 PM
  5. Hibernate subclasses
    By Ed in forum JDBC
    Replies: 2
    Last Post: 07-02-2007, 05:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •