packageorg.training.exceptions;
public classArrayIndexOutOfBounds {
public static void main(String[] args) {
int array[] = new int[3];
for (int i = 0; i < 4; /* this condition checks for more space that allocated for the array */ i++) {
try {
array[i] = i;
}catch(ArrayIndexOutOfBoundsException aiobe) {
System.err.println("Array Index Out Of Bounds Exception has been caught, check your code again");
aiobe.printStackTrace();
}
}
for ( int x : array) {
System.out.println(x);
}
}
}
Output:
0
1
2
Array Index Out Of Bounds Exception has been caught, check your code again
java.lang.ArrayIndexOutOfBoundsException: 3
at org.training.exceptions.ArrayIndexOutOfBounds.main(ArrayIndexOutOfBounds.java:12)
No comments:
Post a Comment