title: Figuring out how class Scanner and its exceptions work together in Java
body: <p>I am looking at the example in <em>How to Program in Java, 7e</em>. 
  User inputs the data manually into object of class <code>AccountRecord record</code></p>
  
  <pre><code>AccountRecord record = new AccountRecord();
  Scanner input = new Scanner( System.in );
  while ( input.hasNext() ) // loop until end-of-file indicator
        {
           try // output values to file
           {
              // retrieve data to be output
              record.setAccount( input.nextInt() ); // read account number
              record.setFirstName( input.next() ); // read first name
              record.setLastName( input.next() ); // read last name
              record.setBalance( input.nextDouble() ); // read balance
  
           .............................................................
  
           catch ( NoSuchElementException elementException )
           {
              System.err.println( "Invalid input. Please try again." );
              input.nextLine(); // discard input so user can try again
           } // end catch
        }
  </code></pre>
  
  <p>I have hard time figuring out how <code>catch ( NoSuchElementException elementException )</code> works. According to Java Documentation, NoSuchElementException  is </p>
  
  <blockquote>
    <p>Thrown by the nextElement method of an Enumeration to indicate that
    there are no more elements in the enumeration.</p>
  </blockquote>
  
  <p>So, why it would also throw an exception in case type mismatch between expected and what is actually entered, such as for <code>record.setAccount(input.nextInt())</code>, user inputs some text string?</p>
  
  <p>Thanks !</p>