Wiki Markup |
---|
A method should never throw {{RuntimeException}} or {{Exception}}. This is because handling these requires catching {{RuntimeException}}, which is forbidden in [EXC32-J. Do not catchCatch specific exceptions as opposed to the more general RuntimeException]. Moreover, throwing a {{RuntimeException}} can lead to subtle errors such as a caller who fails to retrieve a return value from an offending method, is unable to check for appropriate feedback. The Java Language Specification (Section 8.4.7 Method Body) allows the declaration of a method with a return type without making it necessary to return a value if a runtime exception is thrown from within the method \[[JLS 05|AA. Java References#JLS 05]\]. |
...
To handle the case of passing in a null
string parameter, code calling this function may require catching RuntimeException
, which is a violation of EXC32-J. Do not catch Catch specific exceptions as opposed to the more general RuntimeException.
Compliant Solution
...
Wiki Markup |
---|
\[[MITRE 09|AA. Java References#MITRE 09]\] [CWE ID 397|http://cwe.mitre.org/data/definitions/397.html] "Declaration of Throws for Generic Exception", [CWE ID 537|http://cwe.mitre.org/data/definitions/537.html] "Information Leak Through Java Runtime Error Message" \[[Goetz 04b|AA. Java References#Goetz 04b]\] \[[Tutorials 08|AA. Java References#Tutorials 08]\] [Unchecked Exceptions â The Controversy|http://java.sun.com/docs/books/tutorial/essential/exceptions/runtime.html] |
...
EXC32-J. Do not catch Catch specific exceptions as opposed to the more general RuntimeException 11. Exceptional Behavior (EXC) 12. Serialization (SER)