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 
+2

用'continue'交换'break'? – Mena

+0

使用continue进行交换中断会在一次输入后终止循环。 –

+2

Juste消除“休息”应该够了。 – Berger

回答

1

continue声明将重新启动循环(而不是在break声明,终止循环)。

因此如果更换break;continue;,你会不断循环后您Exception被捕获(不提供其他Exception被抛出但是一抓到),ANS显示错误消息。

本质上,它会重新打印"Enter a number"

警告

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

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这里甚至没有必要的,因为你只打印一条警告消息。

+0

谢谢,您能否通过使用扫描仪的下一个值来详细说明您的意思?当我删除break语句后,它会捕获一个会永久打印的异常:输入一个数字,并显示错误消息,但没有机会输入。 –

+0

@MichealBingham不用客气。查看我的编辑:nextDouble调用不会消耗扫描器的下一个值,只能检索它,而nextLine成语则需要用户再次输入并返回。因此,在使用'nextDouble'出错时,您会遇到无限循环,因为它会一直处于失败状态。解决这个问题意味着一个稍微不同的成语(和'Exception'处理程序)。 – Mena

0

break声明导致循环结束。

break声明有两种形式:带标签和无标签。在前面关于switch语句的讨论中,您看到了 未标记的表单。您 也可以使用未标记的突破,以终止一对,同时,还是做,而 循环

解决方案:

改变它continue

continue语句跳过的当前迭代,同时, 或do-while循环。

0

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

注意,Scanner方法(I假设readScanner)如果下一个标记代表一个双nextDouble()返回双精度值。如果没有,它会抛出一个异常而不消耗该令牌。所以,你需要确保你消耗的下一个值,你可以用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); 
+0

当我这样做时,程序将永久循环并询问:输入一个数字,然后显示错误消息,但没有机会输入数字。 –

+0

查看更新的答案。 –

0

还有一个解决方案只是替代read.nextLine();break

break声明导致循环结束。

continue将一次又一次地打印Enter a number

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

如果您删除read.nextLine();continue,代码将会打印输入一个号码。

+0

谢谢!这工作!但我不确定这是为什么工作 –

+0

@MichealBingham,输入令牌不会被消耗,所以你需要再次明确地使用它。 'nextLine()'会做到这一点。 – Satya

-1

只要删除休息;从那里,一切都会正常工作。

不过要记住要终止条件,我不会在任何地方看到它。

+1

为什么标记为负值?请置评论。 –

+0

@Mena这就够了。您不需要继续使用,因为它是连续的循环。它会再次去请求输入。只有终止条件是必需的,因为它是无限循环的。 –

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 
相关问题