哪个应该优先于另一个?例子:
class Example {
int[] A = new A[5];
void setArray(int item, int index) {
if(index < 0 || index >= 5) {
System.out.println("Index out of bounds");
} else {
A[index] = item;
}
}
}
还是这个?
class Example {
int[] A = new A[5];
void setArray(int item, int index) {
try {
A[index] = item;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Index out of bounds");
}
}
}