You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 44 Next »

Methods should rarely throw RuntimeException or Exception, because handling these exceptions requires catching RuntimeException, which is forbidden in guideline [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]].

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

Noncompliant Code Example

This noncompliant code example accepts a string and returns true when it consists of a capital letter followed by lowercase letters. To handle corner cases, it checks for various exceptional conditions and throws exceptions when they are likely to disrupt normal operation.

boolean isCapitalized(String s) {
  if (s == null) {
    throw new RuntimeException("Null String");
  }
  if (s.equals("")) {
    return true;
  }
  String first = s.substring(0, 1);
  String rest = s.substring(1);
  return (first.equals(first.toUpperCase()) &&
          rest.equals(rest.toLowerCase()));
}

To handle the case of passing in a null string parameter, code calling this method may require catching RuntimeException, which is a violation of guideline EXC14-J. Catch specific exceptions as opposed to the more general RuntimeException or Exception.

Compliant Solution

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

boolean isCapitalized(String s) {
  if (s == null) {
    throw new NullPointerException();
  }
  if (s.equals("")) {
    return true;
  }
  String first = s.substring(0, 1);
  String rest = s.substring(1);
  return (first.equals(first.toUpperCase()) &&
          rest.equals(rest.toLowerCase()));
}

Noncompliant Code Example

This noncompliant code example uses a broad Exception class in the throws declaration of the method.

private void doSomething() throws Exception {
  //...
}

Compliant Solution

To be compliant, be as specific as possible when declaring exceptions and respect the required abstraction level.

private void doSomething() throws IOException {
  //...
}

Using instanceof to check for narrower exceptions in a general catch block is not always helpful because it is usually impossible to enumerate all the exceptions that the code is capable of throwing.

Risk Assessment

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

Guideline

Severity

Likelihood

Remediation Cost

Priority

Level

EXC13-J

low

likely

medium

P6

L2

Automated Detection

TODO

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this guideline on the CERT website.

Bibliography

[[MITRE 2009]] CWE ID 397 "Declaration of Throws for Generic Exception", CWE ID 537 "Information Leak Through Java Runtime Error Message"
[[Goetz 2004b]]
[[Tutorials 2008]] Unchecked Exceptions — The Controversy


EXC12-J. Do not log unsanitized user input      06. Exceptional Behavior (EXC)      EXC14-J. Catch specific exceptions as opposed to the more general RuntimeException or Exception

  • No labels