Using System.exit () to call another method within the same class

Is there a reason why I should avoid calling methods in System.exit()

, like in the code example 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());
    }
}

      

+3


source to share


6 answers


The method java.lang.System.exit()

ends the current Java virtual machine.

method declaration java.lang.System.exit()

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

      



where status

is the exit status.

here you are using the value of your method myBank.processClients()

. So the exit value is the return value of the 0

specified method

More details

+3


source


System.exit()

should be used with caution. The normal method for terminating a program is to terminate all user threads.

System.exit()

Suitable cases :

  • service scripts
  • GUI applications in which a custom event dispatch thread is created in the background.

The method System.exit()

forcibly terminates all Java virtual machine threads. This is radical .... System.exit()

should be reserved for a catastrophic exit, or for cases where the program is intended to be used as a utility in a script command that might depend on the program's exit code.

You can call this static method like this:



System.exit(int status);

      

where status

is the exit status.

For example, if status

= 1

, you get:

enter image description here

+3


source


As John said, you don't call processClients

inside System.exit

. The function is run, and then the value returned by that function is then passed in System.exit

as an exit code.

To avoid this, you don't need to avoid it if you are sure that the function will always return the exit code you want.

+2


source


System.exit () is a handy way to handle shutdown in large programs. If someone wants to leave, they can just call System.exit () and it takes care of doing all the necessary closing ceremonies like closing files, freeing resources, etc.

"This method never returns normally." waht is written in the document, which means that the method will not return; once the flow goes there, it will not return.

Please refer to doc

+1


source


System.exit()

used to terminate the current virtual machine (JVM).

The code you provided and similar code will work as long as the method call is passed as an argument, returns its integer (0/1/-1)

as the result, as this indicates that the process exited normally or not.

But why would you need such coding? Since in all the scenarios I can think of, a method call can always be made just before the callSystem.exit(0);

myMethod();
System.exit(0);

      

basically the same as

System.exit(myMethod());

      

While it myMethod()

returns the correct data type, of course.

+1


source


There is no reason to use system.exit and not call the method 1 line above it. system.exit will only return an abnormal shutdown, which can lead to an error. if you have to call this method correctly, you will not need to call system.exit and will be able to find out if there are any errors from your code.

for good practice and debugging. I suggest you use a traditional method call, not system.exit.

+1


source







All Articles