Reputation: 25
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());
}
}
Upvotes: 3
Views: 2126
Reputation: 21
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.
Upvotes: 1
Reputation: 49606
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:
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:
Upvotes: 3
Reputation: 1737
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.
Upvotes: 1
Reputation: 8658
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
Upvotes: 1
Reputation: 4135
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
Upvotes: 3
Reputation: 37
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.
Upvotes: 2