Written by Sai Sandeep Thota on December 31, 2013 in Java > Programming

Java Program to catch ArrayIndexOutOfBoundsException


Get our Latest updates delivered to your mailbox!

ArrayIndexOutOfBoundsException is triggered whenever an user try to store data out of the index of an array.
For example, if the size of the array is 5, so that you can store atmost 6 values in that array (0,1,2,3,4,5), if the user wants to store the data in the 6th position of the array, it will throw an error. So to catch this error we use ArrayIndexOutOfBoundsException.

Size of the Array: 5.
Values in the Array: a[0] = 0, a[1] = 10, a[2] = 20, a[3] = 30, a[4] = 40.
So the above data storage is perfectly alright.
Now if we try to store the data at a[5] then we will get the following error:

Download Source Code

Java Program to catch ArrayIndexOutOfBoundsException

import java.io.*;

class arrayError {
	public static void main(String args[])
		int a[] = new int[5];
			a[0] = 0;
			a[1] = 1;
			a[2] = 20;
			a[3] = 30;
			a[4] = 40;
			a[5] = 50; //This allocation will cause the error!
		catch(ArrayIndexOutOfBoundsException e)


{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: