Java 带异常处理的while循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18838647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
while loop with exception handling
提问by user2745043
This is my first time using exception handling so be gentle. I have a simple blob class that accepts an ID, the id must be between 30 and 50 or else an exception is thrown.
这是我第一次使用异常处理,所以要温柔。我有一个简单的 blob 类,它接受一个 ID,该 ID 必须在 30 到 50 之间,否则会引发异常。
public class Blob {
int id;
public Blob() {
}
public Blob(int id) throws Exception {
this.id = id;
if (id < 30 || id > 50)
throw new Exception ("id = " +id+ ", must be between 30 and 50 inclusive");
}
}
It should prompt the user to enter an id, and throw the exception if it's not between 30 and 50, and should continue until the user enters a valid input and then simply displays the id number.
它应该提示用户输入一个 id,如果它不在 30 到 50 之间,则抛出异常,并且应该继续直到用户输入有效的输入,然后简单地显示 id 号。
public class BlobCreator {
public static void main(String[] args) {
int id;
Scanner scan = new Scanner(System.in);
System.out.println("Enter ID number: ");
id = scan.nextInt();
do {
try {
Blob b = new Blob(id);
}
catch (Exception e) {
System.out.println(e);
}
System.out.println("Enter a different ID: ");
id = scan.nextInt();
}
while(true);
}
System.out.println("Blob ID: " +id);
}
I think that I am using the throw and catch correctly, but my loop isn't working correctly so I think that should be a simple fix but I can't get it just right. Also is using a while loop like I have the best way for this situation or is there a better way to loop through throw and catch?
我认为我正确地使用了 throw 和 catch,但是我的循环不能正常工作,所以我认为这应该是一个简单的修复,但我无法做到恰到好处。还使用 while 循环,就像我对这种情况有最好的方法一样,还是有更好的方法来循环 throw 和 catch?
Thanks for any and all help
感谢您的任何帮助
采纳答案by Willem Van Onsem
You should place the break;
after the code is executed successfully.
您应该将 放在break;
代码执行成功之后。
do {
try {
Blob b = new Blob(id);
break;
}
catch (Exception e) {
System.out.println(e);
}
System.out.println("Enter a different ID: ");
id = scan.nextInt();
} while(true);
So each time the loop would reach the end of its body, it would break out of the loop. You only should break after the blob
is created successfully. Although I dont see why you put a break
anyway. The while
loop can check if the entered input was valid and simply stop the loop.
因此,每次循环到达其主体的末尾时,它都会跳出循环。只有blob
在成功创建后才应该中断。虽然我不明白你为什么要放一个break
。该while
循环可以检查输入的输入是有效的,只是停止循环。
I modified the while
in a do-while
loop... By using true
the loop will run forever, unless no exception is thrown by the constructor... This makes the code more generic (if you modify the conditions of the blob-construction, you don't have to modify the condition of the while
loop).
我while
在do-while
循环中修改了......通过使用true
循环将永远运行,除非构造函数没有抛出异常......这使得代码更通用(如果你修改 blob 构造的条件,你不会必须修改while
循环的条件)。
回答by Joey587
Sorry, its kind of late to the party. Hope users who endup here may find this useful.
The use of break
keyword is discouraged
Here is a very simple implementation to break away after implementing a retry mechanism
This iterates over the loop for the specified number of times and also if the exception still persists, then the exception is thrown. This can be leveraged for an actual real world scenario where the resttemplate
might actually result in IO/Network errors and can be retried in those cases
抱歉,聚会有点晚了。希望最终到这里的用户可能会发现这很有用。break
不鼓励使用关键字 这是一个非常简单的实现,可以在实现重试机制后脱离。它会在循环中迭代指定的次数,如果异常仍然存在,则抛出异常。这可以用于实际的现实世界场景,其中resttemplate
可能实际上导致 IO/网络错误,并且可以在这些情况下重试
public class TestClass {
public static void main(String[] args) throws Exception {
try {
int c = anotherM();
System.out.println("Now the value is" + c);
} catch (Exception e) {
System.out.println("Inside" + e);
}
}
public static int anotherM() throws Exception {
int i = 4;
Exception ex = null;
while (i > 0) {
try {
System.out.println("print i" + i);
throw new IOException();
// return i;
} catch (Exception e) {
System.out.println(e);
i--;
if (i == 1) {
ex = new Exception("ttt");
}
}
}
if (ex != null) {
throw new Exception("all new");
} else {
return i;
}
}
}