...
This implementation uses an internal flag to track whether the exit is permitted. The method setExitAllowed()
sets this flag. The checkExit()
method throws a SecurityException
when the flag is unset (that is, false). Because this flag is not initially set, normal exception processing bypasses the initial call to System.exit()
. The program catches the SecurityException
and performs mandatory cleanup operations, including logging the exception. The System.exit()
method is enabled only after cleanup is complete.
Exceptions
ERR09-J-EX0: It is permissible for a command-line utility to call System.exit()
, for example, when the required number of arguments are not input [Bloch 2008], [ESA 2005].
...
Allowing unauthorized calls to System.exit()
may lead to denial of service.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
ERR09-J | Low | Unlikely | Medium | P2 | L3 |
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
CodeSonar |
| JAVA.DEBUG.CALL | Debug Call (Java) | ||||||
Coverity | 7.5 | DC.CODING_STYLE | Implemented | ||||||
Parasoft Jtest |
| CERT.ERR09.JVM CERT.ERR09.EXIT | Do not stop the JVM in a web component Do not call methods which terminates Java Virtual Machine | ||||||
SonarQube |
| S1147 | Exit methods should not be called |
Related Guidelines
Android Implementation Details
On Android, System.exit()
should not be used because it will terminate the virtual machine abruptly, ignoring the activity life cycle, which may prevent proper garbage collection.
Bibliography
[API 2014] | Method |
Section 9.5, "The Finalize Method" | |
[ESA 2005] | Rule 78, Restrict the use of the |
Section 7.4, "JVM Shutdown" | |
Chapter 16, "Intercepting a Call to |
...
...