0

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.

2
  • You can throw your own run-time exception. Commented Apr 12, 2016 at 23:17
  • Hi, Thanks for replying. i have arounds 20+methods, each will be called sequentially. So If i keep throwing run-time exceptions, is it a good practice? Also as i said in my post, these are not really exceptions but it is just that I dont want to the program to continue and the program should end normally. Commented Apr 13, 2016 at 0:13

3 Answers 3

1

Why not have the methods return a boolean to determine if the next method should run?

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, Thanks for replying. I can certainly have a boolean as a return to determine whether the next method should run or not. But is there any other way to do this without checking a if condition before calling each method?
0

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

Comments

0

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

3 Comments

System.exit(-1) if i understand correctly, will terminate the java program right? Also, i assume it will terminate all the process running in the same JVM, correct? Please correct me if i am wrong.
It will terminate the JVM, yes.
Any non-zero value (e.g. 1) when passed to System.exit(), e.g. System.exit(1) will exit the shutdown the JVM and will indicate a non-normal exit. It need not be -1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.