有关循环内异常(尝试捕获)的问题

时间:2019-11-12 10:20:38

标签: java exception try-catch

我在其他项目上也遇到过几次这个问题。现在,我试图理解异常处理,但是我仍然不知道它是如何工作的。 我尝试使用循环对计算器进行编程,当我尝试输入String时,会出现InputMismatchException-我试图捕获它,但是由于某些原因,在catch子句之后,java给了我另一个InputMismatchException,为什么?

import java.util.InputMismatchException;
import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int loop = 1;
        while (loop == 1) {

            try {

                System.out.println("First number:");
                int num1 = scanner.nextInt();
                System.out.println("Second number:");
                int num2 = scanner.nextInt();

                System.out.println("Choose operator (1 for +)(2 for -)(3 for *)(4 for /): ");

                int userInput = scanner.nextInt();

                switch (userInput) {
                case 1:
                    System.out.println("Result: " + num1 + " + " + num2 + " = " + (num1 + num2));
                    break;
                case 2:
                    System.out.println("Result: " + num1 + " - " + num2 + " = " + (num1 - num2));
                    break;
                case 3:
                    System.out.println("Result: " + num1 + " * " + num2 + " = " + (num1 * num2));
                    break;
                case 4:
                    System.out.println("Result: " + num1 + " / " + num2 + " = " + (num1 / num2));
                    break;
                default:
                    System.out.println("Invalid Input!");

                }

                System.out.println("Repeat? (1 = yes)(0 = nein)");

                loop = scanner.nextInt();

            } catch (InputMismatchException e) {
                System.out.println("Invalid Input, try again!");
                scanner.nextInt();

            }

        }
        scanner.close();
        System.out.println("Ciao!");

    }

}

起初有一个无限循环,我可以通过catch块中的scanner.nextInt();解决这个问题,但仍然出现此错误:

Choose operator (1 for +)(2 for -)(3 for *)(4 for /): 
er
Invalid Input, try again!
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at Calculator.main(Calculator.java:47)

为什么?

4 个答案:

答案 0 :(得分:1)

可能是因为您没有获取nextInt,因为下一个值不是int。因此,您尝试在catch块中再次获取相同的输入,从而再次遇到InputMismatchException。

您的Stacktrace也指向catch子句,您尝试在其中检索下一个int

我建议您将catch子句中的nextInt替换为nextLine

...
catch (InputMismatchException e) {
    System.out.println("Invalid Input, try again!");
    scanner.nextLine();
}

然后,您将再次开始循环并尝试重新阅读。

还在stackoverflow或this上签出tutorialspoint examples for using the Scanner class类似线程。

答案 1 :(得分:0)

在您的第一种情况下,由于InputMismatchException被抛出scanner.nextInt();而进入无限循环,并且您已经处理了该异常,因此它将再次用于下一次迭代,最终再次调用scanner.nextInt();但是在这种情况下,扫描程序不会移至新行,因此它将获取String而不是它原来期望的int并将抛出InputMismatchException。您可以通过以下方法解决:

        catch (InputMismatchException e) {
            System.out.println("Invalid Input, try again!");
            scanner.nextLine(); // change nextInt to nextLine
        }

在您当前的代码中,当InputMismatchException抛出时,它会转到catch块和catch块,然后再次执行scanner.nextInt();,该抛出异常并终止循环线程。

答案 2 :(得分:0)

  1. 捕获异常后,应扫描不匹配输入。您可以使用scanner.nextLine(),然后扫描程序将扫描错误的输入,以便您的循环再次开始。
  2. 您的程序在异常之后重新开始。如果要在异常之后继续循环,则必须使用多次try catch并使用do直到用户输入正确的输入。
Integer num1 = null;
do {
   try {
     num1 = scanner.nextInt();
   } catch (InputMismatchException e) {
     System.out.println("Invalid Input, try again!");
     scanner.nextLine();
   }
} while (num1 == null);

答案 3 :(得分:-1)

如果我们要捕获异常而不是将其抛出方法之外,则可能抛出异常的行应放在try块中,如

try{
//code which can throw exception T
}
catch( exceptions T){
// what if exception handle it
} 

,但在您的情况下为scan.nextInt();在try块中产生异常,并且您在执行同一行Scanner.nextLine();时,控制再次在此处捕获块。 再次引发异常,这次不处理 删除scanner.nextInt();捕获块中的行

相关问题