Try-Catch 异常处理未提供正确的响应
Try-Catch Exception Handling does not provide the correct response
我不确定为什么当我输入整数值以外的任何内容时,输出没有显示 Invalid Format!
。
这就是我要实现的异常处理。另外,如何在 finally
子句中关闭 Scanner
而不会导致无限循环。
class ConsoleInput {
public static int getValidatedInteger(int i, int j) {
Scanner scr = new Scanner(System.in);
int numInt = 0;
while (numInt < i || numInt > j) {
try {
System.out.print("Please input an integer between 4 and 19 inclusive: ");
numInt = scr.nextInt();
if (numInt < i || numInt > j) {
throw new Exception("Invalid Range!");
} else {
throw new InputMismatchException("Invalid Format!");
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
scr.next();
}
}
scr.close();
return numInt;
}
这是我试图获得的输出:
如果您输入除 int 以外的任何内容,将在以下行抛出错误:
numInt = scr.nextInt();
然后陷入catch块,从而跳过打印语句。您需要检查是否有下一个整数:
if(!scr.hasNextInt()) {
throw new InputMismatchException("Invalid Format!");
}
Also, how can I close the Scanner in a finally clause without causing an infinite loop.
您不需要在 finally 块中关闭 Scanner
。事实上,你根本不应该关闭它。关闭 System.in
是不好的做法。通常,如果您没有打开资源,则不应关闭它。
以下代码适合您,这是您可以用来获得所需输出的另一种方法。
在这段代码中,我处理了 catch 块中的 InputMismatch。
public static int getValidatedInteger(int i, int j) {
Scanner scr = new Scanner(System.in);
int numInt = 0;
while (numInt < i || numInt > j) {
try {
System.out.print("Please input an integer between 4 and 19 inclusive: ");
numInt = scr.nextInt();
if (numInt < i || numInt > j) {
System.out.println("Incorrect Range!");
}
} catch (InputMismatchException ex) {
System.out.println("Incorrect format!");
scr.next();
}
}
scr.close();
return numInt;
}
您需要在异常捕获块之前的单独捕获块中捕获 InputMismatchException 并添加 scr.next();如下所示:
public static int getValidatedInteger(int i, int j) {
Scanner scr = new Scanner(System.in);
int numInt = 0;
while (numInt < i || numInt > j) {
try {
System.out.print("Please input an integer between 4 and 19 inclusive: ");
numInt = scr.nextInt();
if (numInt < i || numInt > j) {
throw new Exception("Invalid Range!");
}
} catch (InputMismatchException ex) {
System.out.println("Invalid Format!");
scr.next();
} catch (Exception ex) {
System.out.println(ex.getMessage());
scr.next();
}
}
scr.close();
return numInt;
}
我不确定为什么当我输入整数值以外的任何内容时,输出没有显示 Invalid Format!
。
这就是我要实现的异常处理。另外,如何在 finally
子句中关闭 Scanner
而不会导致无限循环。
class ConsoleInput {
public static int getValidatedInteger(int i, int j) {
Scanner scr = new Scanner(System.in);
int numInt = 0;
while (numInt < i || numInt > j) {
try {
System.out.print("Please input an integer between 4 and 19 inclusive: ");
numInt = scr.nextInt();
if (numInt < i || numInt > j) {
throw new Exception("Invalid Range!");
} else {
throw new InputMismatchException("Invalid Format!");
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
scr.next();
}
}
scr.close();
return numInt;
}
这是我试图获得的输出:
如果您输入除 int 以外的任何内容,将在以下行抛出错误:
numInt = scr.nextInt();
然后陷入catch块,从而跳过打印语句。您需要检查是否有下一个整数:
if(!scr.hasNextInt()) {
throw new InputMismatchException("Invalid Format!");
}
Also, how can I close the Scanner in a finally clause without causing an infinite loop.
您不需要在 finally 块中关闭 Scanner
。事实上,你根本不应该关闭它。关闭 System.in
是不好的做法。通常,如果您没有打开资源,则不应关闭它。
以下代码适合您,这是您可以用来获得所需输出的另一种方法。 在这段代码中,我处理了 catch 块中的 InputMismatch。
public static int getValidatedInteger(int i, int j) {
Scanner scr = new Scanner(System.in);
int numInt = 0;
while (numInt < i || numInt > j) {
try {
System.out.print("Please input an integer between 4 and 19 inclusive: ");
numInt = scr.nextInt();
if (numInt < i || numInt > j) {
System.out.println("Incorrect Range!");
}
} catch (InputMismatchException ex) {
System.out.println("Incorrect format!");
scr.next();
}
}
scr.close();
return numInt;
}
您需要在异常捕获块之前的单独捕获块中捕获 InputMismatchException 并添加 scr.next();如下所示:
public static int getValidatedInteger(int i, int j) {
Scanner scr = new Scanner(System.in);
int numInt = 0;
while (numInt < i || numInt > j) {
try {
System.out.print("Please input an integer between 4 and 19 inclusive: ");
numInt = scr.nextInt();
if (numInt < i || numInt > j) {
throw new Exception("Invalid Range!");
}
} catch (InputMismatchException ex) {
System.out.println("Invalid Format!");
scr.next();
} catch (Exception ex) {
System.out.println(ex.getMessage());
scr.next();
}
}
scr.close();
return numInt;
}