@@ -13,17 +13,20 @@ program state from being written to disk consistently.</p>
1313
1414<p >It is sometimes considered acceptable to call <code >System.exit</code >
1515from a program's <code >main</code > method in order to indicate the overall exit status
16- of the program. Such calls are an exception to this rule.</p >
16+ of the program. The <code >main</code > method should be the primary place
17+ where exit conditions are handled, as it represents the natural termination point
18+ of the application. Such calls are an exception to this rule.</p >
1719
1820</overview >
1921<recommendation >
2022
21- <p >It is usually preferable to use a different mechanism for reporting
22- failure conditions. Consider returning a special value (perhaps
23- <code >null</code >) that users of the current method check for and
24- recover from appropriately. Alternatively, throw a suitable exception, which
25- unwinds the stack and allows properly written code to clean up after itself,
26- while leaving other threads undisturbed.</p >
23+ <p >Instead of calling <code >System.exit</code > from non-main methods, prefer to propagate
24+ errors upward to the <code >main</code > method where they can be handled appropriately.
25+ Consider returning a special value (perhaps <code >null</code >) that users of the current
26+ method check for and recover from appropriately. Alternatively, throw a suitable exception,
27+ which unwinds the stack and allows properly written code to clean up after itself,
28+ while leaving other threads undisturbed. The <code >main</code > method can then catch
29+ these exceptions and decide whether to exit the program and with what exit code.</p >
2730
2831</recommendation >
2932<example >
@@ -33,17 +36,20 @@ to write some data to disk and terminates the JVM if this fails. This
3336leaves the partially-written file on disk without any cleanup
3437code running. It would be better to either return <code >false</code > to
3538indicate the failure, or let the <code >IOException</code > propagate
36- upwards and be handled by a method that knows how to recover.</p >
39+ upwards to the <code >main</code > method where it can be handled appropriately
40+ and the program can exit gracefully if necessary.</p >
3741
3842<p >Problem 2 is more subtle. In this example, there is just one entry point to
3943the program (the <code >main</code > method), which constructs an
4044<code >Action</code > and performs it. <code >Action.run</code > calls
41- <code >System.exit</code > to indicate successful completion. Consider,
42- however, how this code might be integrated in an application server that
43- constructs <code >Action</code > instances and calls
45+ <code >System.exit</code > to indicate successful completion. Instead, the
46+ <code >run</code > method should return a status code or throw an exception
47+ on failure, allowing the <code >main</code > method to decide whether to exit
48+ and with what exit code. Consider how this code might be integrated in an
49+ application server that constructs <code >Action</code > instances and calls
4450<code >run</code > on them without going through <code >main</code >.
4551The fact that <code >run</code > terminates the JVM instead of returning its
46- exit code as an integer makes that use-case impossible.</p >
52+ exit code makes that use-case impossible.</p >
4753
4854<sample src =" CallsToSystemExit.java" />
4955
0 commit comments