3

Is there any reason why I should avoid calling methods in System.exit() like in the example code below?

public class RBS {

    int processClients() {
        ...
        return 0;
    }

    public static void main(String[] args) {
        RBS myBank = new RBS("Royal Bank of Scotland");
        System.exit(myBank.processClients());
    }
}
1
  • 10
    You're not calling processClients "in" System.exit. You're calling processClients, and then you're using the return value as the argument to System.exit. They're entirely independent. Commented Jul 17, 2015 at 15:27

6 Answers 6

3

The java.lang.System.exit() method terminates the currently running Java Virtual Machine.

the declaration for java.lang.System.exit() method

public static void exit(int status)//Should be an int value. 

where status -- This is the exit status.

here you are using value of your method means myBank.processClients(). So exit value is return value 0 of the above method

For more

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

Comments

3

System.exit() should be used with care. The normal method of terminating a program is to terminate all user threads.

Cases in which System.exit() is appropriate:

  • utility scripts
  • GUI applications, in which the event dispatch user thread is created in the background.

The System.exit() method forces termination of all threads in the Java virtual machine. This is drastic....System.exit() should be reserved for a catastrophic error exit, or for cases when a program is intended for use as a utility in a command script that may depend on the program's exit code.

You can call this static method as follows:

System.exit(int status);

where status - exit status.

For example, if status = 1, you'll get:

enter image description here

1 Comment

This answer contains some sensible things, but manages to complete elude the question
2

As Jon said you aren't calling processClients inside of System.exit. The function runs and then the value returned by that function is then passed to System.exit as the exit code.

As for avoiding it, you don't have to avoid it as long as you're sure that the function will always return the exit code that you want.

Comments

1

System.exit() is a convenient way to handle shutdown in bigger programs. If someone wants to quit, he can simply call System.exit(), and it takes care of doing all necessary shutdown ceremonies such as closing files, releasing resources etc.

"This method never returns normally." is waht been written in doc which means that the method won't return; once a thread goes there, it won't come back.

Please refer the doc

Comments

1

System.exit() is used to terminate the currently running virtual machine (JVM).

The code you provided, and similar code will work as long as the method call passed as the argument returns an integer (0/1/-1) as its result as these indicate if the process terminated normally or not.

But why would you ever require such coding? Because, in all the scenarios I can think of, the method call can always be made just before calling System.exit(0);

myMethod();
System.exit(0);

is basically the same as

System.exit(myMethod());

As long as myMethod() returns the correct data type, ofcourse.

1 Comment

Not quite the same. Assuming that myMethod returns an int, then System.exit(myMethod()): is the same as int val = myMethod(); System.exit(val);
1

There is no reason to be using system.exit rather than calling the method 1 line above it. the system.exit will only return an abnormal shutdown, potentially causing an error. if you were to properly call the method, you wouldn't need to call the system.exit and would be able to see if any errors were to result from your code.

for the sake of good practice and debugging I suggest you use a traditional method call rather than the system.exit.

Comments

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.