我已经在我的程序中实现了异常处理,但现在我遇到的问题是,当异常发生并在catch块中处理时,它不是从它发生的地方继续,而是回到程序的开头,所以在catch块中所做的任何更改都是无用的。
简单的例子
public class Example {
public static void main(String[] args) {
int x;
boolean repeat = true;
Scanner input = new Scanner();
do {
try {
x = input.nextInt();
System.out.println("Success!");
repeat = false;
}
catch(InputMismatchException e) {
System.out.println(e.getMesasge());
system.out.println("\nYou must enter an integer");
//BTW am I correct in thinking the following clears the buffer?
input.nextLine();
x = input.nextInt();
}
} while (repeat);
但是如果我这样做了,程序将返回到do块的开头,因此它将重置X的值,而不是从成功消息所在的行继续。
我知道这是因为在那个时刻,重复布尔值为true,因此它会触发while条件重新开始,但是如果我在catch块中将其设置为false,我可能会暴露给自己一个未处理的异常,因为仍然有人可能尝试输入一些无效的东西。
一旦catch块处理了异常,有没有办法在抛出异常的行后返回控制?
发布于 2020-02-13 08:32:34
如果您的应用程序将有多个用户输入,那么您应该有多个循环来处理这些输入中的每一个,因为每个输入都有可能出错并生成异常。
在大多数情况下,如果异常是由于用户输入而发生的,那么保存该输入的变量中的值无论如何都是不需要的,因为它显然是错误的,或者至少已经落到了它的初始默认值。在这种情况下,通常您会希望用户有机会提供正确的输入。如果不是,那么不要一开始就把提示符放在循环中。
Scanner input = new Scanner(System.in);
String ls = System.lineSeparator();
// We want the User to enter an inclusive number from 1 to 5.
int x = 0;
while (x < 1 || x > 5) {
System.out.print("Enter an Integer Number from 1 to 5: --> ");
try {
x = input.nextInt();
input.nextLine(); // Consume ENTER.
System.out.println("Success! The nextInt() method accepted your input!");
if (x < 1 || x > 5) {
System.err.println("But...this application does not accept it!");
System.err.println("We asked for a number from 1 to 5! Try again..." + ls);
}
}
catch (InputMismatchException ex) {
System.out.println(ex.getMessage());
System.err.println("Invalid Input! An Integer number from 1 to 5 only!" + ls);
//BTW am I correct in thinking the following clears the buffer?
/* YES you are since the nextInt() method does not consume the
the newline character provided by the ENTER key like the nextLine()
method does and therefore provides it on the next input request
which in this case ultimately generates an endless loop of exceptions.
Even if this prompt was successful and control is passed on to the
next prompt and if that prompt was a nextLine() method then it would
be basically skipped over because it would then consume that ENTER
key newline character provided in the last nextInt() method. So
with that in mind, it doesn't hurt to place input.nextline();
directly after the x = input.nextInt(); ether. */
input.nextLine(); // Consume ENTER.
}
}
System.out.println(x + " Was Supplied! - DONE!");
尽管有时它也有一定的用途,但我个人尽量避免使用异常。我认为如果可以的话,最好避免使用它们,这就是为什么对于控制台应用程序,我更喜欢使用接受所有键盘输入的Scanner#nextLine()方法,例如:
Scanner input = new Scanner(System.in);
String ls = System.lineSeparator();
// Prompt 1:
// We want the User to enter an inclusive number from 1 to 5.
int x = 0;
String userIN = "";
while (x < 1 || x > 5) {
System.out.print("Enter an Integer Number from 1 to 5 (q to quit): --> ");
userIN = input.nextLine();
if (userIN.toLowerCase().charAt(0) == 'q') {
System.out.println("Quitting!");
System.exit(0);
}
// Is it a signed or unsigned integer number with 1 or more digits?
if (userIN.matches("-?\\d+")) {
System.out.println("Success! The nextLine() method accepted your input" + ls
+ "to be a string representation of an Integer value!");
x = Integer.parseInt(userIN);
}
else {
System.err.println("Invalid Input! An Integer number from 1 to 5 only!" + ls);
continue;
}
if (x < 1 || x > 5) {
System.err.println("But...this application does not accept it!");
System.err.println("We asked for a number from 1 to 5! Try again..." + ls);
}
}
System.out.println(x + " Was Supplied! - DONE!" + ls);
// Prompt 2:
// We want the User to enter any float or double type numerical value.
double d = Double.MIN_VALUE;
while (d == Double.MIN_VALUE) {
System.out.print("Enter a float or double type number (q to quit): --> ");
userIN = input.nextLine().toLowerCase().trim();
if (userIN.charAt(0) == 'q') {
System.out.println("Quitting!");
System.exit(0);
}
// Get rid of the type designator from value if it exists.
if (userIN.endsWith("f") || userIN.endsWith("d")) {
userIN = userIN.substring(0, userIN.length() - 1);
}
// Is it a signed or unsigned integer, float, or double type number?
if (userIN.matches("-?\\d+(\\.\\d+)?")) {
System.out.println("Success! The nextLine() method accepted your input" + ls
+ "to be a string representation of an Integer, float," + ls
+ "or double type value!");
d = Double.parseDouble(userIN);
}
else {
System.err.println("Invalid Input! A Float or Double type numerical value is required!" + ls);
d = Double.MIN_VALUE;
}
}
System.out.println(d + " Was Supplied! - DONE!");
发布于 2020-02-13 05:34:44
如果在没有将repeat
设置为true的情况下从catch
块中删除input.nextLine(); x = input.nextInt();
,则执行将继续到do
的开始处,并要求用户输入值。如果输入了正确的值,则可以将repeat
设置为false以中断do-while
。
https://stackoverflow.com/questions/60200999
复制相似问题