Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by NavBot (vkp) v1.0

Wiki Markup
A method should rarely throw {{RuntimeException}} or {{Exception}}. This is because handling these exceptions requires catching {{RuntimeException}}, which is forbidden in [EXC14-J. Catch specific exceptions as opposed to the more general RuntimeException or Exception]. Moreover, throwing a {{RuntimeException}} can lead to subtle errors, for instance, 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 2005|AA. Java References#JLSBibliography#JLS 05]\].

Instead, prefer throwing a more specific exception, subclassed from Exception. It is permissible to construct an exception class specifically for a single throw statement.

...

Wiki Markup
\[[MITRE 2009|AA. Java References#MITREBibliography#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 2004b|AA. Java References#GoetzBibliography#Goetz 04b]\]
\[[Tutorials 2008|AA. Java References#TutorialsBibliography#Tutorials 08]\] [Unchecked Exceptions — The Controversy|http://java.sun.com/docs/books/tutorial/essential/exceptions/runtime.html]

...