1
I have the code:
for(int i = 0; i < 99999; i++) {
minhaLista.get(i + x); //x é int e possui um valor qualquer
}
At some point the sum of i + x
results in a value greater than the list size.
If you execute this code I will receive a IndexOutOfBoundsException
.
What is the best treatment for this case?
I should check every iteration if (i + x) < minhaLista.size()
? Or should I capture the IndexOutOfBoundsException
?
This is one of the options I put to the question. The question is: why check every iteration instead of capturing the
Exception
?– igventurelli
An exception as the name already says and an exception, is should be treated as such, in its case amenos that x = 0 the Indexoutofboundsexception will always be launched.
– Henrique Luiz