title: How to catch an IllegalArgumentException instead of terminating?
body: <p>I'm trying to get a project done where I have to catch an IllegalArgumentException in my program. It's throwing the exception correctly, but when it throws, the program terminates instead of running my catch and continuing. Not sure what's wrong, I've tried a few different things.</p>
  <p>Here's my main:</p>
  <pre><code>import java.util.Scanner;
  
  public class MonthDaysTest 
  {
    public static void main(String[] args) 
    {
      int month = 1, year = 2020;
      boolean tryAgain = true; //exception thrown test variable
      Scanner input = new Scanner(System.in);
  
      //repeats attempt for input until an exception is not thrown
      do
      {
          try
          {
              //prompts for int representing the month
              System.out.print (&quot;Enter the month (1=January, 2=February, ..., 12=December): &quot;);
              month = input.nextInt();
  
              //prompts for int representing the year
              System.out.print (&quot;Enter the year: &quot;);
              year = input.nextInt();
          
              //sets the test variable to false if an exception is not thrown
              tryAgain = false;
          }
          catch (IllegalArgumentException illegal)
          {                              
              input.nextLine(); //discards input so user can try again
              System.out.printf(&quot;Exception: %s%n%n&quot;, illegal.getMessage());
          }
      }
      while (tryAgain);
  
      MonthDays monthDay = new MonthDays(month, year);
  
      //prints out message with number of days in requested month
      System.out.printf (&quot;%d%s%d%s%d%s&quot;, monthDay.getMonth(), &quot;/&quot;, monthDay.getYear(), &quot; has &quot;, monthDay.getNumberOfDays(), &quot; days.&quot;);
    }
  }
  </code></pre>
  <p>And the relevant part of my MonthDays class:</p>
  <pre><code>public class MonthDays
  {
    private int month, year;
  
    //constructor for class
    public MonthDays(int month, int year)
    {
      setMonth(month);
      setYear(year);
    }                                  
  
    //sets the month while making sure it is valid
    public void setMonth(int month)
    {
      if (month &lt; 1 || month &gt; 12)
      {
        throw new IllegalArgumentException(&quot;Month must be between 1 and 12.&quot;);
      }
      this.month = month;
    }
  
    //sets the year while making sure it is valid in that it is after the implementation of the Gregorian calendar
    public void setYear(int year)
    {
      if (year &lt; 1583)
      {
        throw new IllegalArgumentException(&quot;Year must be 1583 or later.&quot;);
      }
      this.year = year;
    }
  }
  </code></pre>
  <p>When I run the program with an illegal month input, I just get this:</p>
  <pre><code>Exception in thread &quot;main&quot; java.lang.IllegalArgumentException: Month must be between 1 and 12.
      at monthDaysTest.MonthDays.setMonth(MonthDays.java:24)
      at monthDaysTest.MonthDays.&lt;init&gt;(MonthDays.java:15)
      at monthDaysTest.MonthDaysTest.main(MonthDaysTest.java:73)
  C:\Users\jolen\AppData\Local\NetBeans\Cache\9.0\executor-snippets\run.xml:111: The following error occurred while executing this line:
  C:\Users\jolen\AppData\Local\NetBeans\Cache\9.0\executor-snippets\run.xml:94: Java returned: 1
  BUILD FAILED (total time: 3 seconds)
  </code></pre>
  <p>I found another question asking something similar, but I don't see anything I'm doing differently from the answers on that one. That was here: <a href="https://stackoverflow.com/questions/26960941/catching-illegalargumentexception">Catching IllegalArgumentException?</a></p>