Try-Catch异常处理

时间:2014-08-15 10:24:15

标签: java

try {
    System.out.println("how many times");
    rollnumber = scanner.nextInt();
    nigh=2;
 }catch (Exception e){
    System.out.print("invalid. re-enter");
 }     
 }while (nigh==1);

它会不断打印无限多次"无效重复输入"

1 个答案:

答案 0 :(得分:1)

你进入一个无限循环,因为scanner.nextInt();不会消耗错误输入中的字符。更改catch子句,如下所示:

try {
    ... // Your code
} catch (Exception e){
    System.out.print("invalid. re-enter");
    scanner.nextLine();
}