简体   繁体   中英

end of the program java based on condition

I have a requirement where i need to call multiple methods in a sequential manner. But if any one of the method fails due to a validation, the program should not continue. I cannot use "Throw Exception because these are not actually exception rather than a condition that satisfies my requirement and after satisfying it, I don't want the program to continue.

Below is a piece of code for example and understanding. Even i use Return, it still continues to next method.

public void method1(){
    System.out.println("Method 1");
    return;
}
public void method2(){
    System.out.println("Method 2");
    return;
}
public void method3(int a) throws Exception{
    System.out.println("Method 3");
    if (a==3) FinalMethod();
    return;
}
public void method4(){
    System.out.println("Method 4");
    return;
}
public void method5(){
    System.out.println("Method 5");
    return;
}

public void FinalMethod() {
System.out.println("This is the final method - end of the program");
return;
}

public void callMethod() throws Exception{
    method1();
    method2();
    method3(3);
    method4();
    method5();
}

The method callMethod will be called from Main method. Please help me to learn this.

Edited: If The argument is 3 in method3, it should call Finalmethod and after that the program should end. I dont want it to go for method4 and method5.

这些方法为什么不返回一个布尔值以确定下一个方法是否应该运行?

Calling return at the end of a method block is redundant in this scenario.

Assuming that you are looking to terminate the program on error, you can possibly use System.exit(-1) in your catch (if you follow this way), or in the if statement, if this is how you are checking for the error

Edit: I should also clarify that there is no specific meaning to using System.exit(-1) as opposed to using any System.exit(n) where n != 0, unless otherwise specified in your own documentation

This is what's currently going on in in the stack when you call FinalMethod from method3:

main -> callMethod -> method3 -> FinalMethod

So when FinalMethod finishes, you go back to method3, and from there, go back to callMethod, and continue running to the end.

What I would do is make method3 return a boolean if you want to exit and call it with regard to this:

public boolean method3(int a) {
    System.out.println("Method e");
    return a==3;
}

...

//In callMethod

if (method3(3)) { //If we want to exit after running method3
    FinalMethod();
    return;
}

Though you may use System.exit(exitCode), this is not good practice, as it violates the program flow - that the program will only end at the end of the main function.

Though method3 is currently throwing an exception, you don't actually throw one in the method. However, exceptions should only be used for undefined behaviour (particularly relating to circumstances beyond your control, eg. external code). It is preferable to provide a user-friendly error and continue the program if possible, or exit gracefully if not.

Unrelated tips:

  • You do not have to call return at the end of a void function.
  • By default, you should make methods private, and only make them public when required

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