Methods must not throw RuntimeException
or Exception
. Handling these exceptions requires catching RuntimeException
, which is disallowed by rule ERR14-J. Do not catch NullPointerException or any of its ancestors. Moreover, throwing a RuntimeException
can lead to subtle errors, ; for example, a caller cannot examine the exception to determine why it was thrown , and consequently cannot attempt recovery.
...
A calling method must also violate rule ERR14-J. Do not catch NullPointerException or any of its ancestors to determine if the https://www.securecoding.cert.org/confluence/pages/editpage.action?pageId=24608774RuntimeException
RuntimeException
was thrown.
Compliant Solution
...
Note that the null check is redundant; if it were removed, the next call (s.equals("")
) will throw a NullPointerException
when s
is null. However, the explicit null check is a good form , because it explicitly indicates the programmer's intent. More complex code may require explicit testing of invariants and appropriate throw statements.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a0f6aa922059e3ae-6055b189-45454862-8b889a35-efb905bcfea4b6ef6dd65763"><ac:plain-text-body><![CDATA[ | [[MITRE 2009 | AA. Bibliography#MITRE 09]] | [CWE ID -397 | http://cwe.mitre.org/data/definitions/397.html] "Declaration of Throws for Generic Exception" | ]]></ac:plain-text-body></ac:structured-macro> |
| CWE ID -537 "Information Exposure Through Java Runtime Error Message" |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="29759c062281d1cd-d111c8a8-4c764904-8fcba1c7-f480bd6a673cfc30837b1be3"><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="7727ed96d7a6e89f-5de8ebe4-43f84509-85719b76-43666c5b02f857b304e4aa17"><ac:plain-text-body><![CDATA[ | [[Tutorials 2008 | AA. Bibliography#Tutorials 08]] | [Unchecked Exceptions — The Controversy | http://java.sun.com/docs/books/tutorial/essential/exceptions/runtime.html] | ]]></ac:plain-text-body></ac:structured-macro> |
...