Hello,
what is the meaning of System.exit() ?
if System.exit(0) = close the program without error
But, what is the meaning of System.exit(1) ?
Is there is any System.exit(2), System.exit(3), etc ?
Thanks you
Hello,
what is the meaning of System.exit() ?
if System.exit(0) = close the program without error
But, what is the meaning of System.exit(1) ?
Is there is any System.exit(2), System.exit(3), etc ?
Thanks you
— short answer: System.exit(status) tells the JVM to terminate and returns the integer status to the operating system; by convention 0 means “success” and non‑zero means “abnormal” (so and are essentially right). See the Java API for the exact contract. (System.exit / System docs)
What actually happens under the hood: the JVM begins an orderly shutdown. If a security manager exists it may block the exit; all registered shutdown hooks are started (concurrently) and allowed to finish; then the VM halts. If you need to abort the VM immediately (no hooks), Runtime.halt forces termination. Because shutdown is a sequence of steps, a blocked hook or deadlock can delay or prevent exit. (Runtime.exit / shutdown hooks and halt)
Platform notes (useful when choosing codes): Java accepts any int, but many OSes report only a limited range to a parent process — POSIX shells typically expose only the low 8 bits (0–255), so large or negative ints wrap; Windows ExitProcess uses an unsigned 32‑bit code. Keep your exit codes small and document them. (POSIX/_exit behavior, Windows ExitProcess docs)
Practical guidance: document your chosen nonzero codes (0 = success, a few small values for common errors), avoid calling System.exit from library code (it kills the whole JVM — dangerous in servers/containers), prefer throwing exceptions and let your main decide the exit, and use shutdown hooks for cleanup if needed. For security and robustness reasons don’t let untrusted code call exit. (See SEI CERT guidance on preventing untrusted termination.) (SEI CERT guidance)
Acknowledgements: , — correct on convention; — correct to warn about OS-level restrictions.
Jump to Post— kal_crazy 13any integer will work so that the system can exit. A zero value in exit() will cause no action taken to the error. A non-zero value in exit() will deal with the error using a routine that you need to write.
Hope that helps :)
any integer will work so that the system can exit. A zero value in exit() will cause no action taken to the error. A non-zero value in exit() will deal with the error using a routine that you need to write.
Hope that helps :)
When you execute any program (eg from a batch file or script) the program returns an integer return code; by convention this is 0 for successful execution. The batch file or script can then test that value to select an appropriate next thing to do.
System.exit(n) just makes the JRE finish exceuting and return the value n as its return code. n can be any int value.
A word of caution though, even though the System.exit method accepts the entire gamut of integer values, there are certain restrictions imposed by the host systems (operating systems) on what kind of status codes are acceptable.
Take a look here: http://en.wikipedia.org/wiki/Exit_status
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.