Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Methods must not throw RuntimeException, Exception, or Exception Throwable. Handling these exceptions requires catching RuntimeException, which is disallowed by rule ERR08-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.

...

The isCapitalized() method in this noncompliant code example accepts a string and returns true when it the string consists of a capital letter followed by lowercase letters. The method also throws a RuntimeException when passed a null string argument.

...

A calling method must also violate rule ERR08-J. Do not catch NullPointerException or any of its ancestors to determine if whether the RuntimeException was thrown.

...

This compliant solution throws the (NullPointerException) to denote the specific exceptional condition.

...

Note that the null check is redundant; if it were removed, the next subsequent call (to s.equals("")) will would throw a NullPointerException when s is null. However, the explicit null check is a good form suggested because it explicitly indicates the programmer's intent. More complex code may require explicit testing of invariants and appropriate throw statements.

...

Code Block
bgColor#ccccff
private void doSomething() throws IOException {
  //...
}

Exceptions

EXC07ERR07-EX0: Classes that sanitize exceptions to comply with a security policy are permitted to translate specific exceptions into more general exceptions. This translation could potentially result in throwing RuntimeException, Exception, or Exception Throwable in some cases, depending on the details of the security policy.

Risk Assessment

Throwing RuntimeException and , Exception, or Throwable prevents classes from catching the intended exceptions without catching other unintended exceptions as well.

...

Related Guidelines

MITRE CWE

CWE-397, ". Declaration of Throws for Generic Exception" throws for generic exception

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="cd0fdb03d7ee3d69-68910ee1-4abb4e65-b4728c6a-c481c76cafa587dc5fe6159d"><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="596d826f04a60c85-8aaf2071-40f5491f-a28ab6ce-49dc74ad32f904e6cac00aa2"><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>

...