如何在不抛出 Java 异常的情况下打破 try/catch 块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6534072/
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
How can I break from a try/catch block without throwing an exception in Java
提问by Basil Musa
I need a way to break from the middle of try/catch block without throwing an exception. Something that is similar to the break and continue in for loops. Is this possible?
我需要一种方法来从 try/catch 块的中间中断而不抛出异常。类似于 for 循环中的 break 和 continue 的东西。这可能吗?
I have been getting weird throughts about throwing a custom exception (naming it "BreakContinueException") that simple does nothing in its catch handler. I'm sure this is very twisted.
关于抛出自定义异常(将其命名为“BreakContinueException”),我对它的捕获处理程序中的任何操作都很奇怪。我敢肯定这是非常扭曲的。
So, any straight forward solution I'm not aware of?
那么,任何我不知道的直接解决方案?
采纳答案by aioobe
The proper way to do it is probably to break down the method by putting the try-catch block in a separate method, and use a return statement:
正确的做法可能是通过将 try-catch 块放在单独的方法中来分解方法,并使用 return 语句:
public void someMethod() {
try {
...
if (condition)
return;
...
} catch (SomeException e) {
...
}
}
If the code involves lots of local variables, you may also consider using a break
from a labeled block, as suggested by Stephen C:
如果代码涉及大量局部变量,您也可以考虑使用break
来自标记块的 a,如Stephen C所建议的:
label: try {
...
if (condition)
break label;
...
} catch (SomeException e) {
...
}
回答by Jigar Joshi
how about return
it will do, if there is no finally
怎么return
办,如果没有finally
回答by Aaron Digulla
There are several ways to do it:
有几种方法可以做到:
Move the code into a new method and
return
from itWrap the try/catch in a
do{}while(false);
loop.
移动代码到一个新的方法,并
return
从中将 try/catch 包装在一个
do{}while(false);
循环中。
回答by dacwe
You can always do it with a break
from a loop construct or a labeled break
as specified in aioobies answer.
您始终可以使用break
循环结构中的循环结构或break
aioobies 答案中指定的标记来执行此操作。
public static void main(String[] args) {
do {
try {
// code..
if (condition)
break;
// more code...
} catch (Exception e) {
}
} while (false);
}
回答by Stephen C
Various ways:
各种方式:
return
break
orcontinue
when in a loopbreak
to label when in a labeled statement (see @aioobe's example)break
when in a switch statement.
return
break
或continue
循环时break
在标记语句中进行标记(参见@aioobe 的示例)break
在 switch 语句中时。
...
...
System.exit()
... though that's probably not what you mean.
System.exit()
...虽然这可能不是你的意思。
In my opinion, "break to label" is the most natural (least contorted) way to do this if you justwant to get out of a try/catch. But it could be confusing to novice Java programmers who have never encountered that Java construct.
在我看来,如果您只想摆脱 try/catch ,“打破标签”是最自然(最少扭曲)的方法。但是对于从未遇到过该 Java 构造的新手 Java 程序员来说,这可能会令人困惑。
But while labels are obscure, in my opinion wrapping the code in a do ... while (false)
so that you can use a break
is a worse idea. This will confuse non-novices as well as novices. It is better for novices (and non-novices!) to learn about labeled statements.
但是虽然标签很模糊,但在我看来,将代码包装在 a 中do ... while (false)
以便您可以使用 abreak
是一个更糟糕的主意。这将混淆非新手和新手。新手(和非新手!)最好学习标记语句。
By the way, return
works in the case where you need to break out of a finally
. But you should avoid doing a return
in a finally
block because the semantics are a bit confusing, and liable to give the reader a headache.
顺便说一句,return
在您需要突破finally
. 但是你应该避免return
在finally
块中做 a ,因为语义有点混乱,并且容易让读者头疼。
回答by Fariba
In this sample in catch block i change the value of counter and it will break while block:
在 catch 块中的这个示例中,我更改了计数器的值,它会在块中中断:
class TestBreak {
public static void main(String[] a) {
int counter = 0;
while(counter<5) {
try {
counter++;
int x = counter/0;
}
catch(Exception e) {
counter = 1000;
}
}
}
}k
回答by Gisway
This is the code I usually do:
这是我通常做的代码:
try
{
...........
throw null;//this line just works like a 'break'
...........
}
catch (NullReferenceException)
{
}
catch (System.Exception ex)
{
.........
}