Array index out of bounds exception Java program

//java program to demonstrate array index out of bounds exception


// Program for exception handling in java

 

Description


This program demonstrates ArrayIndexOutOfBoundsException in Java. This type of error occurs when an array is full and cannot include a new element. This program declares an array nos[] of size 3, values are allocated to each array indexes. The statement nos[3]=3; produces an error, because the array is of size 3 and there is no location nos[3], as specified in the statement. 

 

Program


 
public class Bound

{

public static void main(String s[])

{

int nos[]=new int[3];

try

{

nos[0]=0;

nos[1]=1;

nos[2]=2;

nos[3]=3;

}

catch(ArrayIndexOutOfBoundsException ex)

{

System.out.println("Error!! Array Out of Bound");

}

}

}


Output

java program - array index out of bounds exception
Array index out of bounds exception

No comments: