0

捕获异常后,如何继续执行 Java 程序?

我制作了一个程序,要求用户输入一个数字,它将返回该数字除以生成的随机数。但是,如果用户输入像“a”这样的字母,则会捕获异常。

如何让程序继续执行而不是在捕获异常后终止?

do{                     //Begin of loop 
    try{                        //Try this code 
        System.out.println("Enter a number"); 
        double i = read.nextDouble(); //Reads user input  
        double rn = r.nextInt(10);   //Generates random number rn 
        System.out.println(i +  " divided by random number " + rn + " is " + (i/rn)); 
    }catch(InputMismatchException type_error){         //Catches error if there is a type mismatch
        //Example error: if user enters a letter instead of a double
         System.out.println("Error. You cannot divide a letter by a number!"); 
        break;  //break stops the execution of the program 
    } 

    //using a continue statement here does not work 
}while(true);       //Loop forever 
4

6 回答 6

2

继续没有帮助!如果你使用 Scanner 类输入数字,你会得到一个无限循环,写着“错误。你不能用数字除一个字母!” 到输出。

您的代码等待一个双数字,但收到了一封信。此事件触发异常,代码显示错误信息。但是该字母将保留在扫描仪中,因此 nextInt 命令会尝试在下一次迭代中加载相同的字母,而无需等待您输入。

在 catch 块中,您必须使用 read.next() 命令清空扫描仪。

    Scanner read = new Scanner(System.in);
    Random r = new Random();

    do {                     //Begin of loop 

        try {                        //Try this code 
            System.out.println("Enter a number");
            double i = read.nextDouble(); //Reads user input  
            double rn = r.nextInt(10);   //Generates random number rn 
            System.out.println(i + " divided by random number " + rn + " is " + (i / rn));

        } catch (InputMismatchException type_error) {         //Catches error if there is a type mismatch
            //Example error: if user enters a letter instead of a double
            System.out.println("Error. You cannot divide a letter by a number!");

            // Empty the scanner before the next iteration: 
            read.next();
        }

    //using a continue statement here does not work 
    } while (true);       //Loop forever 
于 2016-01-27T17:12:36.697 回答
1

continue语句将重新启动循环(与break终止循环的语句相反)。

因此,如果您替换break;continue;,您将在您Exception被捕获后继续循环(前提是没有其他Exception抛出,但捕获的除外),并显示错误消息。

本质上它会再次打印"Enter a number",等等。

警告

您还需要使用Scanner的下一个值。如果您不这样做,则 usingcontinue将在发生错误时永远循环,无需用户交互。

例子

Scanner read = new Scanner(System.in);
do {
    try {
        // trimmed out non-necessary stuff
        System.out.println("Enter a number");
        double i = Double.parseDouble(read.nextLine());
        System.out.println(i);
        // changed exception caught
    } 
    catch (NumberFormatException type_error) {
        System.out.println("Error. You cannot divide a letter by a number!");
        continue;
    }

} while (true);

最后说明

正如Berger所提到的,continue这里甚至没有必要,因为您只是在打印一条警告消息。

于 2016-01-27T16:53:17.983 回答
0

break语句导致循环退出。try由于-构造之后的循环中没有代码,因此catch您可以简单地删除该break语句。

请注意,如果下一个标记表示双精度,则该Scanner方法(我假设read是 a ScannernextDouble()返回双精度值。如果不是,它会抛出异常而不消耗该令牌。因此,您需要确保使用下一个值,您可以使用read.next()

do{                     //Begin loop 

    try{                        //Try this code 
        System.out.println("Enter a number"); 
        double i = read.nextDouble(); //Reads user input  
        double rn = r.nextInt(10);   //Generates random number rn 
        System.out.println(i +  " divided by random number " + rn + " is " + (i/rn)); 


    }catch(InputMismatchException type_error){         //Catches error if there is a type mismatch
    //Example error: if user enters a letter instead of a double
         System.out.println("Error. You cannot divide " + read.next() + " by a number!"); 

    } 



} while(true); 
于 2016-01-27T16:56:52.513 回答
0

break语句导致循环结束。

声明有两种形式:有标签的break和无标签的。您在前面对 switch 语句的讨论中看到了未标记的形式。您还可以使用未标记的中断来终止for、while 或 do-while 循环

解决方案 :

将其更改为continue.

continue语句跳过 for、while 或 do-while 循环的当前迭代。

于 2016-01-27T16:54:28.207 回答
0

另一种解决方案是替换breakread.nextLine();.

break语句导致循环结束。

continueEnter a number将一次又一次地打印。

但是read.nextLine();,代码会一次又一次地询问输入。

如果您删除read.nextLine();or continue,代码将一次又一次地打印 Enter a number 。

于 2016-01-27T16:58:35.200 回答
-1

只需删除中断;从那里开始,一切都会好起来的。

但是请记住要设置终止条件,我在任何地方都看不到它。

于 2016-01-27T17:05:38.523 回答