...
Code Block |
---|
public static void main(String[] args) {
try {
BadNewInstance.undeclaredThrow(new IOException("Any checked exception"));
}catch(Exception e) {
if (e instanceof IOException) {
System.out.println("IOException occurred");
} else if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else {
//some other unknown checked exception
}
}
|
Compliant Solution
Wiki Markup |
---|
Prefer {{Constructor.newInstance}} over {{Class.newInstance}}. An alternative is to use the builder interface recommended by \[[Bloch 08|AA. Java References#Bloch 08]\]. |
Code Block | ||
---|---|---|
| ||
// Generic type for a builder used to build any object of type T
public interface Builder<T> {
public T build();
}
|
A client can pass a builder to a method and request the creation of an object. A bounded wildcard type should be used to constrain the builder's type parameter. In the code snippet that follows, a US Dollar (USD) is built from coins of different denomination.
Code Block |
---|
USD buildCurrency(Builder<? extends denomination> currencyBuilder) { /* ... */ }
|
For further details on implementing the builder pattern, refer to OBJ32-J. Do not allow partially initialized objects to be accessed. In the example describe in that rule, the Currency.Builder
class must implement the Builder interface highlighted in this recommendation.
Noncompliant Code Example
...
Wiki Markup |
---|
It is also possible to disassemble a class, remove any declared checked exceptions and reassemble the class so that checked exceptions are thrown at runtime when this class is used \[[Roubtsov 03|AA. Java References#Roubtsov 03]\]. Simply, compiling against a class that declares the checked exception and supplying one at runtime that doesn't also suffices. Similarly, a different compiler than {{javac}} might handle checked exceptions differently. Yet another way is to furtively use the {{sun.corba.Bridge}} class. All these methods are strongly discouraged. |
Compliant Solution
Refrain from employing code that is capable of throwing undeclared checked exceptions (legitimate or hostile). If the source code can throw them, explicitly document the behavior. Prefer Constructor.newInstance
over Class.newInstance
. Finally, do not use deprecated methods such as Thread.stop()
.
...
Wiki Markup |
---|
\[[JLS 05|AA. Java References#JLS 05]\] Chapter 11: Exceptions
\[[Venners 03|AA. Java References#Venners 03]\] "Scalability of Checked Exceptions"
\[[Roubtsov 03|AA. Java References#Roubtsov 03]\]
\[[Schwarz 04|AA. Java References#Schwarz 04]\]
\[[Goetz 04b|AA. Java References#Goetz 04b]\]
\[[Naftalin 06b|AA. Java References#Naftalin 06b]\] "Principle of Indecent Exposure"
\[[Bloch 08|AA. Java References#Bloch 08]\] Item 2: "Consider a builder when faced with many constructor parameters"
\[[MITRE 09|AA. Java References#MITRE 09]\] [CWE ID 703|http://cwe.mitre.org/data/definitions/703.html] "Failure to Handle Exceptional Conditions", [CWE ID 248|http://cwe.mitre.org/data/definitions/248.html] "Uncaught Exception" |
...