停止在 Java 中执行更多代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18041770/
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
Stop executing further code in Java
提问by Matt Smith
I have looked in the Javadoc but couldn't find information related to this.
我查看了 Javadoc,但找不到与此相关的信息。
I want the application to stop executing a method if code in that method tells it to do so.
如果该方法中的代码告诉应用程序停止执行该方法,我希望该应用程序停止执行该方法。
If that sentence was confusing, here's what I want to do in my code:
如果这句话令人困惑,这就是我想在我的代码中做的事情:
public void onClick(){
if(condition == true){
stopMethod(); //madeup code
}
string.setText("This string should not change if condition = true");
}
So if the boolean condition
is true, the method onClick
has to stop executing further code.
因此,如果布尔condition
值为真,则该方法onClick
必须停止执行进一步的代码。
This is just an example. There are other ways for me to do what I am trying to accomplish in my application, but if this is possible, it would definitely help.
这只是一个例子。还有其他方法可以让我在我的应用程序中完成我想要完成的工作,但如果可能的话,它肯定会有所帮助。
采纳答案by Tarun Varshney
Just do:
做就是了:
public void onClick() {
if(condition == true) {
return;
}
string.setText("This string should not change if condition = true");
}
It's redundant to write if(condition == true)
, just write if(condition)
(This way, for example, you'll not write =
by mistake).
写是多余的if(condition == true)
,只是写if(condition)
(这样,例如,你不会=
写错)。
回答by axelcdv
You can just use return
to end the method's execution
您可以只用于return
结束方法的执行
回答by Alex MDC
Either return;
from the method early, or throw
an exception.
要么return;
来自方法早,要么throw
异常。
There is no other way to prevent further code from being executed short of exiting the process completely.
除了完全退出进程之外,没有其他方法可以防止执行更多代码。
回答by Zaw Than oo
There are two way to stop current method/process :
有两种方法可以停止当前方法/进程:
- Throwing Exception.
- returnning the value even if it is void method.
- 抛出异常。
- 即使它是 void 方法也返回值。
Option : you can also kill the current thread to stop it.
选项:您也可以杀死当前线程以停止它。
For example :
例如 :
public void onClick(){
if(condition == true){
return;
<or>
throw new YourException();
}
string.setText("This string should not change if condition = true");
}
回答by Jose Sutilo
return
to come out of the method execution, break
to come out of a loop execution and continue
to skip the rest of the current loop. In your case, just return
, but if you are in a for loop, for example, do break
to stop the loop or continue
to skip to next step in the loop
return
退出方法执行,break
退出循环执行并continue
跳过当前循环的其余部分。在您的情况下,只是return
,但例如,如果您处于 for 循环中,请break
停止循环或continue
跳到循环中的下一步
回答by lewis4u
To stop executing java code just use this command:
要停止执行 java 代码,只需使用以下命令:
System.exit(1);
After this command java stops immediately!
执行此命令后 java 立即停止!
for example:
例如:
int i = 5;
if (i == 5) {
System.out.println("All is fine...java programm executes without problem");
} else {
System.out.println("ERROR occured :::: java programm has stopped!!!");
System.exit(1);
}