Versions Compared

Key

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

...

Code Block
bgColor#FFCCCC
try {
    int i = 0;
    while(true) {
         a.doSomething();
         a[i++].next();
    }
} catch (ArrayIndexOutOfBoundsException e) {
    // end of array, ongoing routine
}

Wiki MarkupThis is not recommended because it didn't comply with the main purpose of using exception, which is to detect and recover \[[EXC03-J. Try to gracefully recover from system errors|EXC03-J. Try to gracefully recover from system errors]\]. 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 standard code block in Java VM. It will prevents certain optimizations that JVM would otherwise perform.

Compliant Solution

A standard way of using for idiom can solve the problem.

...