[英]try-catch statement not returning to try block when catching exception
我有一个写的方法。 此方法只扫描用户输入的整数输入。 如果用户输入字符值,它将抛出输入不匹配异常,这在我的Try-Catch语句中处理。 问题是,如果用户输入任何不是数字的东西,然后抛出异常,我需要循环回来再次询问用户输入。 据我所知,如果发现错误,Try catch语句会自动循环回Try块。 这不正确吗? 请指教。
这是我的方法(很简单):
public static int getMask() {
//Prompt user to enter integer mask
Scanner keyboard = new Scanner(System.in);
int output = 0;
try{
System.out.print("Enter the encryption mask: ");
output = keyboard.nextInt();
}
catch(Exception e){
System.out.println("Please enter a number, mask must be numeric");
}
return output;
}//end of getMask method
以下是该方法在我的程序中的实现方式:
//get integer mask from user input
int mask = getMask();
System.out.println("TEMP mask Value is: " + mask);
/ * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * * /这是我更新的代码。 它创造了一个我无法逃脱的无限循环。 我不明白为什么我这么挣扎。 请帮忙。
public static int getMask() {
//Prompt user to enter integer mask
Scanner keyboard = new Scanner(System.in);
int output = 0;
boolean validInput = true;
do{
try {
System.out.print("Enter the encryption mask: ");
output = keyboard.nextInt();
validInput = true;
}
catch(InputMismatchException e){
System.out.println("Please enter a number, mask must be numeric");
validInput = false;
}
}while(!(validInput));
return output;
/ * ** * ** * ** * ** * ** * **** / FINAL_ANSWER我终于得到了它。 我想我只需要更多地学习布尔逻辑。 有时它让我头晕。 用整数测试实现循环工作正常。 我想我自己的用户错误。 这是我的最终代码正常工作,更好的异常处理。 如果您有任何批评,请在评论中告诉我。
//get integer mask from user input
int repeat = 1;
int mask = 0;
do{
try{
mask = getMask();
repeat = 1;
}
catch(InputMismatchException e){
repeat = 0;
}
}while(repeat==0);
据我所知,如果发现错误,Try catch语句会自动循环回Try块。 这不正确吗?
不,这不正确,我很好奇你是如何达成这种理解的。
你有几个选择。 例如(这不会按原样工作,但我们首先讨论错误处理,然后在下面阅读):
// Code for illustrative purposes but see comments on nextInt() below; this
// is not a working example as-is.
int output = 0;
while (true) {
try{
System.out.print("Enter the encryption mask: ");
output = keyboard.nextInt();
break;
}
catch(Exception e){
System.out.println("Please enter a number, mask must be numeric");
}
}
其中包括; 你选择的选项通常取决于你喜欢的口味(例如fge的答案是相同的想法,但略有不同),但在大多数情况下直接反映你想要做的事情:“继续询问,直到用户输入有效数字。 “
另请注意,与fge一样,您通常应该抓住您准备处理的最严重的异常。 nextInt()
会抛出一些不同的异常,但您的兴趣特别在于InputMismatchException
。 您不准备处理,例如, IllegalStateException
- 更不用说如果抛出意外异常会使调试/测试变得困难,但是您的程序假装它们只是与无效输入相关(因此从不通知您有不同的问题)发生了)。
现在,那就是说, Scanner.nextInt()
在这里有另一个问题,如果它不能被解析为整数,则令牌留在流上。 如果您不从流中取出该令牌,这将使您陷入循环。 为此,你实际上想要使用next()
或nextLine()
,这样nextLine()
总是使用令牌; 然后你可以用Integer.parseInt()
解析,例如:
int output = 0;
while (true) {
try{
System.out.print("Enter the encryption mask: ");
String response = keyboard.next(); // or nextLine(), depending on requirements
output = Integer.parseInt(response);
break;
}
catch(NumberFormatException e){ // <- note specific exception type
System.out.println("Please enter a number, mask must be numeric");
}
}
请注意,这仍然直接反映了您想要做的事情:“在用户输入有效数字之前继续询问,但无论输入什么,都要消耗输入。”
据我所知,如果发现错误,Try catch语句会自动循环回Try块。 这不正确吗?
这确实不正确。 try
块只会执行一次。
您可以使用它来“解决”它(虽然JasonC的答案更加可靠 - 请继续使用):
boolean validInput = false;
while (!validInput) {
try {
System.out.print("Enter the encryption mask: ");
output = keyboard.nextInt();
validInput = true;
}
catch(Exception e) {
keyboard.nextLine(); // swallow token!
System.out.println("Please enter a number, mask must be numeric");
}
}
return output;
进一步说明:您不应该捕获Exception
而是更具体的异常类。
正如评论中所述,try-catch -blocks不会循环。 如果要循环,请使用for
或while
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.