title: How do I trap NumberFormatException thrown by parseInt
body: <p>I am stuck on this question~:</p>
  
  <p><strong>The method Integer.parseInt converts a String of digits to the int number it denotes.</strong></p>
  
  <pre><code>public static int parseInt (String s)
               throws NumberFormatException
  </code></pre>
  
  <p><strong>Explain how you could trap the exception thrown by parseInt and illustrate your answer with a code example.</strong></p>
  
  <p>My answer that I have thought of so far is:</p>
  
  <pre><code>try
  {
     System.out.println("incorrect");
  
  }
  
  catch (NumberFormatException n)
  {
     System.out.println("WRONG! This is not a number");
  }
  </code></pre>
  
  <p>I have a feeling this answer is wrong because the code above would only be used in the main method and I'm not sure how I would trap the exception under:</p>
  
  <pre><code>public static int parseInt (String s)
               throws NumberFormatException.
  </code></pre>
  
  <p>I've looked through java API for the parseInt method and looked via various research and tried doing the code, however I'm still struggling to get this right due to my lack of understanding.</p>
  
  <p>If anyone could help me out, I'd be grateful, thanks in advance.</p>