title: how to catch a NoSuchElementException?
body: <p>My class assignment is to write a program that has the user input a set of numerical values. If the user enters a value that is not a number, the program is supposed to give the user 2 second chances to enter a number correctly, and after those two chances, quit asking for input and print the sum of all values entered correctly so far.</p>
  
  <p>As is, my code doesn't work quite right. When the first non-number is entered, the program executes the code in the catch block once, prints the "gimme input" line at the beginning of the try block but then <strong>immediately</strong> executes the code in the catch block again without waiting for the user to enter another number.</p>
  
  <p>While perusing my textbook for clues, I noticed this line: "A NoSuchElementException is <strong>not caught</strong> by any of the catch clauses. The exception remains thrown until it is caught by another try block or the main method terminates."</p>
  
  <p>Which is great, because now at least I know there's a good reason this is happening, but my textbook doesn't contain any further information on this quirk, and I haven't been able to find any understandable answers via StackOverflow or Google. So my question is two part:</p>
  
  <p>a) How should I get around this for the purposes of this assignment?</p>
  
  <p>b) What exactly does it mean that the exception is not caught by a catch clause? Isn't that what catch clauses exist for? I do want a solution to my assignment, but I also want to understand <strong>why</strong> this is the way it is, if possible.</p>
  
  <p>Thanks for any help!</p>
  
  <pre><code>import java.util.InputMismatchException;
  import java.util.NoSuchElementException;
  import java.util.ArrayList;
  import java.util.Scanner;
  
  public class NotANumber {
      public static void main(String[] args) {
          Scanner in = new Scanner(System.in);
          System.out.println("This program computes the sum of any number of real numbers. When you are done entering values, enter something other than a real number twice in a row.\n");
  
          ArrayList&lt;Double&gt; numbers = new ArrayList&lt;Double&gt;();
  
          int count = 0;
          while (count &lt; 2) {
              try {
                  System.out.println("Please enter a floating point number: ");
                  double newNumber = in.nextDouble();
                  numbers.add(newNumber);
                  count = 0;
              }
              catch (NoSuchElementException exception) {
                  System.out.println("The value entered was not correctly specified. All values must be integers or floating point values.");
                  count++;
              }
          }
  
          if (!numbers.isEmpty()) {
              double sum = 0;
              for (Double each : numbers) {
                  sum = sum + each;
              }
              System.out.println("The sum is " + sum);
          }
          else {
              System.out.println("There is no sum as no correctly specified values were entered.");
          }
  
      }
  }
  </code></pre>