java - 如何捕获 IllegalArgumentException 而不是终止?
问题描述
我正在尝试完成一个项目,我必须在我的程序中捕获 IllegalArgumentException。它正确地抛出异常,但是当它抛出时,程序终止而不是运行我的 catch 并继续。不知道出了什么问题,我尝试了一些不同的方法。
这是我的主要内容:
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.");
}
}
我的 MonthDays 课程的相关部分:
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;
}
}
当我使用非法月份输入运行程序时,我得到了这个:
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)
我发现另一个问题问类似的问题,但我没有看到我所做的与那个答案不同。那是在这里:Catching IllegalArgumentException?
解决方案
在构造函数中,您调用的方法可以通过异常:
public MonthDays(int month, int year)
{
setMonth(month);
setYear(year);
}
您还需要将构造函数调用放在 try catch 块中:
MonthDays monthDay = new MonthDays(month, year);
像 :
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);
推荐阅读
- python - 张量流中的错误
- c++ - 如何组装一串宽字符,中间插入一些空字符?
- mysql - mysqld 超过 200% CPU
- python - Pandas 数据框从父列和子列中删除最后一个“\”
- android - Xamarin Forms Android App 一直隐藏操作栏
- javascript - JSON POST 请求 - 接收 JSON 对象
- json - 如何将嵌套的 JSON 数组解析为 Codable 结构
- java-8 - Java 8:forEach 子句中的检查异常
- c - 根据C中二维数组中的列中的值重新排列行
- visual-studio - Visual Studio 和自定义 msbuild 任务