简体   繁体   中英

How to break/exit different levels of method-calling in Java

Let's say I have:

public void one() {
  two();
  // continue here
}
public void two() {
  three();
}
public void three() {
  // exits two() and three() and continues back in one()
}

Are there any methods to doing this?

The only way to do this without change method two() is to throw an Exception.

If you can change the code you can return a boolean which tells the caller to return.

However the simplest solution is to inline the methods into one larger method. If this is too large you should retsructure it another way and not place complex controls between methods like this.


Say you have

public void one() {
    System.out.println("Start of one.");
    two();
// do something
    System.out.println("End of one.");
}

public void two() {
    System.out.println("Start of two.");
    three();
// do something
    System.out.println("End of two.");
}

public void three() {
    System.out.println("Start of three.");
// do something
    System.out.println("End of three.");
}

You can add an unchecked exception if you cannot change two();

public void one() {
    System.out.println("Start of one.");
    try {
        two();
    } catch (CancellationException expected) {
    }
// do something
    System.out.println("End of one.");
}

public void two() {
    System.out.println("Start of two.");
    three();
// do something
    System.out.println("End of two.");
}

public void three() {
    System.out.println("Start of three.");
// do something
    System.out.println("End of three.");
    throw new CancellationException(); // use your own exception if possible.
}

You can return a boolean to say return, if you can change two()

public void one() {
    System.out.println("Start of one.");
    two();
// do something
    System.out.println("End of one.");
}

public void two() {
    System.out.println("Start of two.");
    if (three()) return;
// do something
    System.out.println("End of two.");
}

public boolean three() {
    System.out.println("Start of three.");
// do something
    System.out.println("End of three.");
    return true;
}

Or you can inline the structures

public void one() {
    System.out.println("Start of one.");
    two();
// do something
    System.out.println("End of one.");
}

public void two() {
    System.out.println("Start of two.");
    System.out.println("Start of three.");
// do something for three
    System.out.println("End of three.");
    boolean condition = true;
    if (!condition) {
// do something for two
        System.out.println("End of two.");
    }
}

Looking at your code, if you call one, it then calls two, which calls three.. If you leave it as it is, thats exactly what it WILL do. The line after two (in your one) function, will be only done once its come back from two, and it wont do that until two has finished with three..

Assuming you can change the two() method, perhaps you want something like this?

public void one() {
    two();
    // continue here from condition
}

public void two() {
    if (three()) {
        // go back due to condition 
        return;
    }

    // condition wasn't met
}

public boolean three() {
    // some condition is determined here

    if (condition) {
        // exits two() and three() and continues back in one()
        return true;
    }

    // condition wasn't met, keep processing here

    // finally return false so two() keeps going too
    return false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM