...
| Code Block | ||
|---|---|---|
| ||
try {
int i = 0;
while(true)
a[i++].next();
} catch (ArrayIndexOutOfBoundsException e) {
// end of array, ongoing routine
}
|
| Wiki Markup |
|---|
This 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]\]. 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
| Code Block | ||
|---|---|---|
| ||
for (int i=0; i<a.length; i++) {
a[i].f();
}
|
...