I have looked in the Javadoc but couldn\'t find information related to this.
I want the application to stop executing a method if code in that method tells it to do
You can just use return
to end the method's execution
Either return;
from the method early, or throw
an exception.
There is no other way to prevent further code from being executed short of exiting the process completely.
There are two way to stop current method/process :
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");
}
To stop executing java code just use this command:
System.exit(1);
After this command java stops immediately!
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);
}
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).
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