ArrayIndexOutOfBoundsException

In this example we are going to see how we can catch
the exception ArrayIndexOutOfBoundException. ArrayIndexOutOfBoundException
is thrown when we have to indicate that an array has been accessed with an
illegal index.
Suppose we have declared an array of int and the size
of the array is 6, that means that this array can store six values. Now
suppose if want to access the seventh variable which does not exist, then it
will throws the exception ArrayIndexOutOfBound. It means that there is no other
value instead after that we are forcing the array to give the next
value.
The code of the program is given below:
<HTML>
<HEAD>
<TITLE>Catching an ArrayIndexOutOfBoundsException</TITLE>
</HEAD>
<BODY>
<H1> ArrayIndexOutOfBoundsException in jsp</H1>
<%
try{
int a[] =new int[6];
for(int i = 0; i<7; i++){
a[i]=i;
}
}catch(Exception e){
out.println("Exception:"+e);
}
%>
</BODY>
</HTML>
|
Output of the Program:

Download this example.

|