Versions Compared

Key

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

Exceptions should only be used to denote exceptional conditions. They should not be used for ordinary control flow purposes.

Noncompliant Code Example

This noncompliant code example attempts to concatenate a few string elements of array c and store the result as the first element. It uses an ArrayIndexOutOfBoundsException to detect the end of the array and initialize the value of variable i to 0 in the catch block. However, if some element of the array is null, a NullPointerException results. This exception is caught and ignored, a violation of EXC09-J. Do not catch NullPointerException. Consequently, the variable i is not initialized to 0 as expected

Wiki Markup
{{exception}} is used to improve program reliability by detecting exceptional situation and possibly recover from it. However, it should not be used abusively to act as part of routine functionality. Irregular usage of exception can obfuscate the purpose of code, mask real bug and degrade system performance \[[Effective Jave, item 39|http://books.google.com/books?id=ZZOiqZQIbRMC&pg=PA169&lpg=PA169&dq=item+39+use+exceptions&source=bl&ots=UZP26ufK15&sig=E60s65pIGPpYY7O79U96dx_oAqg&hl=en&ei=QLHzSsPFMMOZlAe3ioSiAw&sa=X&oi=book_result&ct=result&resnum=1&ved=0CAgQ6AEwAA#v=onepage&q=item%2039%20use%20exceptions&f=false]\].

Noncompliant Code Example

In the following example, the program tries to use ArrayIndexOutOfBoundsException to detect the end of array and then proceed with its ongoing logic.

Code Block
bgColor#FFCCCC
try {
    int i = 0;
  String c[] = new String[3];
c[0] = "value1";
c[1] = "value2";
c[2] = "value3";
	
int i;
c[1] = null; // gets null value

try {
  i = 0;
  while(true) {
	         
     a.doSomething();
     c[0] = c[0].concat(c[i + 1]); // Concatenate and store in c[0]  
    a[i++].next();
    }
} catch (ArrayIndexOutOfBoundsException e) {
  i = 0; // end of array, ongoing routine
}
 Attempts to initialize i to 0
} catch (NullPointerException npe) {
  // Ignores
}

The real purpose of exception handling is to detect and recover from exceptional conditions and not to willfully transfer control flow. Besides, performance wise, the This is not recommended because it didn't comply with the main purpose of using exception, which is to detect and recover. The normal logical flow of reaching the end of array is treated as an exception here, which has an inverse effect on readability and comprehension. Besides, exception-based idiom is far slower than the standard code block in Java VM. It will also prevents certain optimizations that JVM would otherwise perform.

Compliant Solution

A standard way of using for idiom can solve the problemThis compliant solution uses a standard for loop to concatenate the strings.

Code Block
bgColor#ccccff
for (int i=0; i<a.length; i++) {
    a[i].doSomething();
}

Noncompliant Code Example

In another sense, an exposed API should not force its clients to use exceptions for ordinary control flow. A class that has state-dependent method should also have a state-testing method to accompany it, so as to decide whether the first method should be called. In the following example, because iterator class didn't has a hasNext() method, the caller is forced to use exception to detect the end of iterator.

Code Block
bgColor#FFCCCC

try {
    Iterator i = collection.iterator();
    while(true) {
         Foo foo = (foo) i.next();
    }
} catch (NoSuchElementException e) {
    // ongoing routine
}

Compliant Solution

A simple fix will be to implement a hasNext() method in the iterator class.

Code Block
bgColor#ccccff

for (Iterator i = collection.iterator(); i.hasNext();) {
    Foo foo = (Foo) i.next();
    ...
}

Risk Assessment

String c[] = new String[3];
c[0] = "value1";
c[1] = "value2";
c[2] = "value3";

int i;
for (i = 1; i < c.length; i++) {
  c[0] = c[0].concat(c[i]);
}
i = 0; // Initialize i to 0 after operation

Risk Assessment

The use of exceptions for anything but detecting and handling exceptional conditions can result in performance degradation and poor The abusive usage of exception can result in performance degrade and poor logical flow design.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

EXC10- J

low

unlikely

medium

P2

L3

...

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

References

Wiki Markup
\[[Effective Jave, item 39|http://books.google.com/books?id=ZZOiqZQIbRMC&pg=PA169&lpg=PA169&dq=item+39+use+exceptions&source=bl&ots=UZP26ufK15&sig=E60s65pIGPpYY7O79U96dx_oAqg&hl=en&ei=QLHzSsPFMMOZlAe3ioSiAw&sa=X&oi=book_result&ct=result&resnum=1&ved=0CAgQ6AEwAA#v=onepage&q=item%2039%20use%20exceptions&f=false]\]
\[[Java ReferenceBloch 01|AA. Java References#Bloch 01]\] Item 39: "Use exceptions only for exceptional conditions"
\[[JLS 05|AA. Java References#JLS 05]\]

...