...
This noncompliant code example also violates ERR13-J. Do not throw RuntimeException, Exception, or Throwable. It falls under EX0 of ERR14-J. Do not catch NullPointerException , RuntimeException, Exception, or Throwableor any of its ancestors.
| Code Block | ||
|---|---|---|
| ||
public class NewInstance {
private static Throwable throwable;
private NewInstance() throws Throwable {
throw throwable;
}
public static synchronized void undeclaredThrow(Throwable throwable) {
// These exceptions should not be passed
if (throwable instanceof IllegalAccessException ||
throwable instanceof InstantiationException) {
throw new IllegalArgumentException(); // Unchecked, no declaration required
}
NewInstance.throwable = throwable;
try {
// next line throws the Throwable argument passed in above,
// even though the throws clause of class.newInstance fails
// to declare that this may happen
NewInstance.class.newInstance();
} catch (InstantiationException e) { /* unreachable */
} catch (IllegalAccessException e) { /* unreachable */
} finally { // Avoid memory leak
NewInstance.throwable = null;
}
}
}
public class UndeclaredException {
public static void main(String[] args) { // No declared checked exceptions
NewInstance.undeclaredThrow(new Exception("Any checked exception"));
}
}
|
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="bb4c4f9b5d81c4f0-6e5bd647-41d84d33-8198b85f-96722760a464f31d2b13a4e7"><ac:plain-text-body><![CDATA[ | [[MITRE 2009 | AA. Bibliography#MITRE 09]] | [CWE-703 | http://cwe.mitre.org/data/definitions/703.html] "Improper Check or Handling of Exceptional Conditions" | ]]></ac:plain-text-body></ac:structured-macro> |
| CWE-248 "Uncaught Exception" |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="8d7c5ab510c3f54f-2154deb3-40d24cc2-8aeb9196-3b3ceec1d2db3c4835f045e3"><ac:plain-text-body><![CDATA[ | [[Bloch 2008 | AA. Bibliography#Bloch 08]] | Item 2: "Consider a builder when faced with many constructor parameters" | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a9511122d4f4a3b5-4b31de8b-4af44fb8-963d9e72-37891e6f978d2971f9b3f2e1"><ac:plain-text-body><![CDATA[ | [[Goetz 2004b | AA. Bibliography#Goetz 04b]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="dd722725b9a38f85-3d12d01c-460149e8-80178804-514bbcb8338f942e3f7582ba"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. Bibliography#JLS 05]] | Chapter 11: Exceptions | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="cbcaef4b039edad1-e01e801d-4d7a4628-b1f8bbe0-62e37fac5f4130d4f4cd923d"><ac:plain-text-body><![CDATA[ | [[Roubtsov 2003 | AA. Bibliography#Roubtsov 03]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a55cf00b619d13fb-cc8eaea8-4202450e-8113be65-eecb56749f99cf402006f718"><ac:plain-text-body><![CDATA[ | [[Schwarz 2004 | AA. Bibliography#Schwarz 04]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="be8540b93df2f8e6-768247a6-419747d6-a2d683dc-0030fe0b4dd2a69d783d724d"><ac:plain-text-body><![CDATA[ | [[Venners 2003 | AA. Bibliography#Venners 03]] | "Scalability of Checked Exceptions" | ]]></ac:plain-text-body></ac:structured-macro> |
...