I'm really confused: The standard approach in Java is to throw exceptions only in "abnormal" conditions and not to use them to signal end-of-iterator.
examples: Effective Java, item 57 ("Use exceptions only for exceptional conditions") and JavaSpecialists newsletter 162:
Flow control
We should never cause an exception that is otherwise preventable. I have seen code where instead of checking bounds, it is assumed that the data will be correct and then RuntimeExceptions are caught:
Here is an example of bad code (please don't code like this):
public class Antipattern1 { public static void main(String[] args) { try { int i = 0; while (true) { System.out.println(args[i++]); } } catch (ArrayIndexOutOfBoundsException e) { // we are done } } }
whereas it is standard to use this idiom in Python, e.g. StopIteration:
exception StopIteration
Raised by an iterator‘s next() method to signal that there are no further values. This is derived from Exception rather than StandardError, since this is not considered an error in its normal application.
Why is it bad for Java but good for Python?