title: Catching IllegalArgumentException?
body: <p>I am having a little bit of a problem here. I am trying to figure out how to catch the IllegalArgumentException. For my program, if the user enters a negative integer, the program should catch the IllegalArgumentException and ask the user if he/she wants to try again. But when the exception is thrown, it doesn't give that option. It just terminates. I tried to use the try and catch method but it doesn't work for me. How do I catch this particular exception to continue to run instead of terminating?</p>
  
  <pre><code>public static void main(String[] args) throws IllegalArgumentException
  {
      String keepGoing = "y";
      Scanner scan = new Scanner(System.in);
      while(keepGoing.equals("y") || keepGoing.equals("Y"))
      {
          System.out.println("Enter an integer: ");
          int val = scan.nextInt();
          if (val &lt; 0)
          {
              throw new IllegalArgumentException
              ("value must be non-negative");
          }
          System.out.println("Factorial (" + val + ") = "+ MathUtils.factorial(val));
          System.out.println("Another factorial? (y/n)");
          keepGoing = scan.next();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>and </p>
  
  <pre><code>public class MathUtils
  {
      public static int factorial(int n)
      {
          int fac = 1;
          for(int i = n; i &gt; 0; i--)
          {
              fac *= i;
          }
          return fac;
      }
  }
  </code></pre>