Java Reference
In-Depth Information
} catch (TimeoutException te) {
// Handle error
} catch (SecurityException se) {
// Handle error
}
Applicability
Exceptions are used to handle exceptional conditions. If an exception is not caught, the
program will be terminated. An exception that is incorrectly caught or is caught at the
wrong level of recovery will often cause incorrect behavior.
Bibliography
[JLS 2013]
Chapter 11, “Exceptions”
[Long 2012]
ERR08-J. Do not catch NullPointerException or any of its ancestors
34. Try to gracefully recover from system errors
According to the JLS, §11.1.1, “The Kinds of Exceptions” [JLS 2013],
The unchecked exceptions classes are the class RuntimeException and its sub-
classes, and the class Error and its subclasses. All other exception classes are
checked exception classes.
Unchecked exception classes are not subject to compile-time checking because it is te-
dious to account for all exceptional conditions and because recovery is often difficult or
impossible. However,even when recovery is impossible, the Java Virtual Machine (JVM)
allows a graceful exit and a chance to at least log the error. This is made possible by us-
ing a try-catch block that catches Throwable . Also, when code must avoid leaking po-
tentially sensitive information, catching Throwable is permitted. In all other cases, catch-
ing Throwable is not recommended because it makes handling specific exceptions diffi-
cult.Wherecleanupoperationssuchasreleasingsystemresourcescanbeperformed,code
should use a finally block to release the resources or a try -with-resources statement.
Catching Throwable is disallowed in general by The CERT ® Oracle ® Secure Coding
Standard for Java [Long 2012], “ERR08-J. Do not catch NullPointer-Exception or
any of its ancestors,” but it is permitted when filtering exception traces by the exception
ERR08-EX0 in that rule.
Search WWH ::




Custom Search