title: how to catch input exception for a scanner
body: <p>I need to scan a number and then sort it in a list. How can i make the program ignore if a user types in letters instead of a number.
  it throws</p>
  <pre class="lang-none prettyprint-override"><code>Exception in thread &quot;main&quot; java.util.InputMismatchException.
  </code></pre>
  <p>code:</p>
  <pre><code>public static void InsertionSort() {
  
          LinkedList&lt;Integer&gt; list = new LinkedList&lt;&gt;();
          Scanner scan = new Scanner(System.in);
          
          int x=-2;
          while(x!=-1)
          {   
              System.out.println(&quot;type a number to place in a sorted list&quot;);
              x=scan.nextInt();
          }
          scan.close();
      }
  </code></pre>