[英]Continue the program execution in multiple try-catch statements scenario after catching exception
[英]Continue Program Execution after Catching Exception
捕獲異常后,如何繼續執行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
繼續將無濟於事! 如果使用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
continue
語句將重新啟動循環(與break
語句相對,后者將終止循環)。
因此,如果您替換break;
continue;
,您將在捕獲到您的Exception
后繼續循環播放(假設沒有其他Exception
拋出,而是捕獲了一個Exception
),並且顯示錯誤消息。
本質上,它將再次打印"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
此處。
break
語句導致循環結束。
break
語句有兩種形式:帶標簽的和不帶標簽的。 您在前面對switch語句的討論中看到了未標記的形式。 您還可以使用無標簽的中斷來終止 for,while或do-while循環
解決方案:
更改它以continue
。
在
continue
語句跳過的當前迭代,while或do-while循環。
break
語句導致循環退出。 在try
- catch
構造之后,由於循環中沒有代碼,因此只需刪除break
語句即可。
請注意,如果下一個標記表示一個double值,則Scanner
方法(假設read
是Scanner
) nextDouble()
返回double值。 如果不是,它將拋出一個異常而不消耗該令牌。 因此,您需要確保使用下一個值,您可以使用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);
read.nextLine();
一種解決方案是將break
替換為read.nextLine();
。
break
語句導致循環結束。
continue
將打印再次Enter a number
。
但是read.nextLine();
,代碼將一次又一次地詢問輸入。
如果刪除read.nextLine();
或continue
,代碼將打印一次又一次輸入數字。
刪除中斷即可; 從那里開始,一切都會正常。
但是請記住,要設置終止條件,我在任何地方都看不到它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.