Can System.exit () return without ending the JVM?

In practice, of course, this is unlikely to happen, but: Is there any reason for calling this method not to interrupt the JVM?

Is it possible, for example, that OutOfMemoryErroris called immediately after (or while) a call, System.exit(0)or what happens when this method is called StackOverflowError, because the stack is simply full at this moment?

+4
source share
2 answers

Is there a reason for calling this method not to interrupt the JVM?

Yes.

From the docs:

Throws:

SecurityException - if there is a security manager, and its checkExit method does not allow exit with the specified status.

+8
source

System.exit()

public static void main(String[] args)
{
    try
    {
        main(args);  // recursion
    }
    catch (StackOverflowError e1)
    {
        System.out.println("OK.");
    }

    try
    {
        System.exit(0);
    }
    catch (StackOverflowError e2)
    {
        System.out.println("HA!");
    }
}

e1, println() exit() , .

+1

Source: https://habr.com/ru/post/1615443/


All Articles