Reputation: 21
I am unable to surround the following if statement with try and catch statement. I still see the ArrayIndexOutOfBoundsException
error.
Scanner sc = new Scanner(System.in);
Scanner sc1 = new Scanner(System.in);
int i = sc.nextint();
int w = sc.nextint();
try {
if(i == 2 && w == 3)
{
System.out.println(“Error. Please enter the missing elements.”);
}
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println(“Error. Please try again.”);
}
Upvotes: 0
Views: 65
Reputation: 650
You are not doing anything with array in mentioned if statement. Exception is caused somewhere else.
Upvotes: 2
Reputation: 131496
A ArrayIndexOutOfBoundsException
exception that is thrown is a bug in your program that you have to correct. It is not a exception to catch.
You should never refer to an index outside the boundaries of an array.
And whatever, as mentioned this code doesn't thrown any ArrayIndexOutOfBoundsException
.
Upvotes: 1