System.exit(status),>> This method never returns normally. What does it means

StackOverflow https://stackoverflow.com/questions/23381780

  •  12-07-2023
  •  | 
  •  

Domanda

I know in we can terminate JVM with System.exit(), but in java doc ,they written as >>

"This method never returns normally.". What does it means?

È stato utile?

Soluzione

This means program stops its execution from that line of code and it will not execute return statement and will even not call finally block

Altri suggerimenti

The documented purpose of this method is to stop the application.

Notes:

  • even finally blocks are prevented from executing.
  • the shutdown hooks are still run.

I assume the question is;

When does System.exit(n) return?

When you have an application server, running multiple applications, you don't want one to bring down the whole JVM. In this situation you typically have a SecurityManager which prevents a number of operations esp System.exit(). In this situation, a SecurityException can be thrown, or the JVM shuts down the specific application being run.

After the call System.exit() your application will terminate immediately not executing and code following the exit() statement. Look at this example:

public static void main(String[] args) {
    System.out.println("about to exit");
    System.exit(1);
    System.out.println("returned normally");
}

OUTPUT:

about to exit

The second statement System.out.println("returned normally") got never called.

It means that the next line of code after System.exit won't be executed (because the JVM was shut down before getting there).

It means the program doesn't cleanly return from where it us executing. An exception thrown is also a case where the program doesn't 'cleanly' return.. As in executing through the end. Exit call abruptly terminates the execution flow.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top