23

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 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.

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.

4
  • 2
    condition = true is an assignment expression. Watch out for the distinction. Commented Aug 4, 2013 at 10:20
  • 3
    (condition = true) is always true.
    – Maroun
    Commented Aug 4, 2013 at 10:23
  • that's just an example. sorry for the typo. fixed it.
    – Matt Smith
    Commented Aug 4, 2013 at 10:42
  • 4
    Correct me if I am wrong... but the title of question appears to be misleading... it says "Java", while description has Javascript problem to be precise. Also the tag is of Java, while it should have been of javascript Commented Jan 16, 2019 at 6:18

8 Answers 8

45

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).

20

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

0
19

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);
    }
1
  • 6
    System.exit() will stop the entire server (tested on Tomcat) or the application. Commented Jan 17, 2018 at 12:08
8

There are two way to stop current method/process :

  1. Throwing Exception.
  2. returnning the value even if it is void method.

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");
}
4

You can just use return to end the method's execution

2

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.

0

In your thread, you need to check Thread.interrupted(), and if you have a flag too as some others mentioned, that'd be handy too. All based on your specific use case.

-1

I think just using return; would do the job

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.