Java 为什么我的try-catch在用于捕获错误时打印出错误和我的catch块?
我目前正在学习try-catch代码块,我正在编写一段代码,接收用户的输入并检查它是否为整数。我的代码是Java 为什么我的try-catch在用于捕获错误时打印出错误和我的catch块?,java,try-catch,Java,Try Catch,我目前正在学习try-catch代码块,我正在编写一段代码,接收用户的输入并检查它是否为整数。我的代码是 int classSize = 0; boolean error = false; do { try{ System.out.println("Hello! Please enter how many Students you have in your class: "); classSize = in.nextInt(
int classSize = 0;
boolean error = false;
do {
try{
System.out.println("Hello! Please enter how many Students you have in your class: ");
classSize = in.nextInt();
error = false;
}
catch(InputMismatchException e){
System.out.println("Invalid input. please make sure you enter numbers ONLY! No symbols or letters.");
classSize = in.nextInt();
error = true;
}
}while(error);
当我试图通过输入字母来测试它时,我会得到错误消息和try block消息
Hello! Please enter how many Students you have in your class:
asdfsda
Invalid input. please make sure you enter numbers ONLY! No symbols or
letters.
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at ExcerciseOne.main(ExcerciseOne.java:65)
为什么它会显示catch块和错误消息?我是否正确使用了try-catch块 您正在捕获try子句中抛出的异常,但随后再次从catch中的扫描器读取,从而抛出另一个异常,这次是未捕获的异常
classSize = in.nextInt();
存在于两个子句中,两次都会引发异常,但仅在从try子句内部引发时才会被捕获。catch块中的in.nextInt调用读取try块中第一次调用in.nextInt留下的坏流
您可以做的是删除catch块中的.nextInt调用:
或者更好:
由于InputMismatchException是RuntimeException的一个子类,引发此错误的错误类型可以通过if语句过滤掉。因为您只需要一个适当的整数作为输入,所以可以使用正则表达式验证输入
boolean hasError = true;
String integerRegex = "[\\d]+"; // it means 1-to-many digits
while (hasError) {
System.out.println("Hello! Please enter how many Students you have in your class: ");
String input = in.nextLine();
if (input.matches(integerRegex)) {
classSize = Integer.parseInt(input);
hasError = false;
} else {
System.out.println("Invalid input. please make sure you enter numbers ONLY! No symbols or letters.");
// variable hasError remains true
}
}
宪兵队的答案很清楚,我为您添加了一个正确的演示:
public static void main(String[] args) {
int studentsCount = 0;
boolean hasError;
do {
try {
System.out.println("Hello! Please enter how many Students you have in your class: ");
Scanner in = new Scanner(System.in);
studentsCount = in.nextInt();
hasError = false;
} catch (InputMismatchException e) {
System.out.println("--> Invalid input. please make sure you enter numbers ONLY! No symbols or letters.");
hasError = true;
}
} while (hasError);
System.out.println("There are " + studentsCount + " students in your class.");
}
查看引发异常的行。我打赌它不在试球区,而是在接球区。好的。我明白问题所在,但如何解决呢?我基本上希望用户在使用catch块重新输入数字时使用它。我必须在试捕中进行试捕吗?这就是循环的目的,对吗?如果失败,它将返回try子句。我认为在catch子句中没有理由要求输入。在不同类型的异常中有很好的相关性。
public static void main(String[] args) {
int studentsCount = 0;
boolean hasError;
do {
try {
System.out.println("Hello! Please enter how many Students you have in your class: ");
Scanner in = new Scanner(System.in);
studentsCount = in.nextInt();
hasError = false;
} catch (InputMismatchException e) {
System.out.println("--> Invalid input. please make sure you enter numbers ONLY! No symbols or letters.");
hasError = true;
}
} while (hasError);
System.out.println("There are " + studentsCount + " students in your class.");
}