Reputation: 13
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.
Here's my main:
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 ("Enter the month (1=January, 2=February, ..., 12=December): ");
month = input.nextInt();
//prompts for int representing the year
System.out.print ("Enter the year: ");
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("Exception: %s%n%n", illegal.getMessage());
}
}
while (tryAgain);
MonthDays monthDay = new MonthDays(month, year);
//prints out message with number of days in requested month
System.out.printf ("%d%s%d%s%d%s", monthDay.getMonth(), "/", monthDay.getYear(), " has ", monthDay.getNumberOfDays(), " days.");
}
}
And the relevant part of my MonthDays class:
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 < 1 || month > 12)
{
throw new IllegalArgumentException("Month must be between 1 and 12.");
}
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 < 1583)
{
throw new IllegalArgumentException("Year must be 1583 or later.");
}
this.year = year;
}
}
When I run the program with an illegal month input, I just get this:
Exception in thread "main" java.lang.IllegalArgumentException: Month must be between 1 and 12.
at monthDaysTest.MonthDays.setMonth(MonthDays.java:24)
at monthDaysTest.MonthDays.<init>(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)
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: Catching IllegalArgumentException?
Upvotes: 1
Views: 1185
Reputation: 1524
The code which throws the exception needs to be inside try-catch block to be catched. In your case new MonthDays(month, year);
throws IlligalArgumantException. You should place MonthDays monthDay = new MonthDays(month, year);
line to before tryAgain = false;
statement.
Upvotes: 0
Reputation: 51583
In the constructor you are calling the methods can through the exception:
public MonthDays(int month, int year)
{
setMonth(month);
setYear(year);
}
You need to put the constructor call also inside the try catch block:
MonthDays monthDay = new MonthDays(month, year);
like :
MonthDays monthDay;
do
{
try
{
//prompts for int representing the month
System.out.print ("Enter the month (1=January, 2=February, ..., 12=December): ");
month = input.nextInt();
//prompts for int representing the year
System.out.print ("Enter the year: ");
year = input.nextInt();
monthDay = new MonthDays(month, year)
//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("Exception: %s%n%n", illegal.getMessage());
}
}
while (tryAgain);
Upvotes: 1