Java 只有在 try 和 catch 块中没有抛出异常时才运行代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30019453/
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
Java run code only if no exception is thrown in try and catch block?
提问by Muhammad Umer
How do I make it so the code runs only if there was no exception thrown?
如何使代码仅在没有抛出异常的情况下运行?
With finally code runs whether there was an exception or not.
无论是否有异常,最终代码都会运行。
try {
//do something
} catch (Exception e) {}
//do something only if nothing was thrown
采纳答案by khelwood
Here are two ways:
这里有两种方法:
try {
somethingThatMayThrowAnException();
somethingElseAfterwards();
} catch (...) {
...
}
Or if you want your second block of code to be outside the try
block:
或者,如果您希望第二个代码try
块在该块之外:
boolean success = false;
try {
somethingThatMayThrowAnException();
success = true;
} catch (...) {
...
}
if (success) {
somethingElseAfterwards();
}
You could also put the if
statement in a finally
block, but there is not enough information in your question to tell if that would be preferable or not.
您也可以将if
语句放在一个finally
块中,但是您的问题中没有足够的信息来判断这是否更可取。
回答by ThePerson
try {
doSomething();
doSomething2();
} catch (Exception e) {
doSomething3();
}
In this example, doSomething2()
will only be executed if no exception is thrown by doSomething()
.
在这个例子中,doSomething2()
只有在没有抛出异常时才会执行doSomething()
。
If an exception is thrown by doSomething()
, doSomething2();
will be skipped and execution will jump to doSomething3();
如果由 抛出异常doSomething()
,doSomething2();
将跳过并执行将跳转到doSomething3();
Also note, doSomething3()
will be executed if there is an exception thrown by doSomething2();
还要注意,doSomething3()
如果有抛出异常,将被执行doSomething2();
If no exception is thrown, doSomething3();
will not be executed.
如果没有抛出异常,doSomething3();
则不会被执行。
回答by pathfinderelite
Just put the code in the try
block. If an exception is thrown, it will skip to the catch
block. If no exception is thrown, the code will just run.
只需将代码放入try
块中即可。如果抛出异常,它将跳到catch
块。如果没有抛出异常,代码将直接运行。
try {
someMethodThatMayThrowException();
codeThatShouldBeRunIfNoExceptionThrown();
} catch (Exception e) {...}
回答by Adrian
Exceptions for flow control is kind of a bad practice. If you insist, use a boolean
variable.
流量控制的例外是一种不好的做法。如果您坚持,请使用boolean
变量。
boolean thrown = false;
try {
//do something
} catch (Exception e) {
thrown = true;
}
//do something only if nothing was thrown
if (!thrown) {
// do stuff
}
回答by GhostCat
An enhancement to the proposed
对提议的改进
try {
somethingThatMayThrowAnException();
somethingElseAfterwards();
} catch (...) {
...
}
from the accepted answer. What you should do is:
从接受的答案。你应该做的是:
void foo() {
try {
doStuff();
} catch (...) {
handleException();
}
}
The above feels like overkill to someone who hasn't been exposed to "clean code thinking".
对于没有接触过“干净的代码思维”的人来说,上述感觉有点过分了。
But the point here: you do notwant to mix different abstractions within one method. In other words: you don't have one try block, and more code following behind that within the same method.
但这里的问题:你不希望一个方法中使用不同的抽象。换句话说:你没有一个 try 块,在同一个方法中后面还有更多的代码。
You make sure that each and any method contains a straight forward path - you avoid anything that complicates your reading flow. As soon as you get used to writing and reading suchkind of code you will find that it takes you dramatically less time to understand your code.
你确保每一种方法都包含一个直接的路径——你避免任何使你的阅读流程复杂化的事情。一旦您习惯了编写和阅读此类代码,您就会发现理解代码所需的时间大大减少。