Thursday, November 3, 2016

ArrayIndexOutOfBoundsException In Java

In This article,we will know how to resolve the ArrayIndexOutOfBoundsException in java.As a programmer,should have checked for in your code such as ArrayIndexOutOfBoundsException where you should have paid attention to the size of the array. Since,this is one of the frequently raised runtime exception while developing projects.

It is a child class of Runtime Exception and it is unchecked thrown automatically by the JVM whenever we are accessing an array element with invalid int index that means out of range index.

When to raised ArrayIndexOutOfBoundsException:

The individual element in the array can be accessed with an index number. The index number always begins with zero,so for any array of ten objects the index numbers will run from 0 to 9. Suppose we create an array three Books as fallows:

    Book b=new Book[3];

At this time,we have one array object on the heap,with three null reference of type Book,but we don't have any Book objects.Now, we will create some Book objects and assign them index positions in the array referenced by b:

b[0]=new Book();
b[1]=new Book();
b[2]=new Book();

This code puts three new Book objects on the heap and assign them to the three index positions in the b array. When ever we tries to access the an out of range index then JVM automatically will arise ArrayIndexOutOfBoundsException. For Example,if an array has three elements trying to access the[3] element will raise ArrayIndexOutOfBoundsException
because in an array of three elements, the legal index values are 0,1 and 2. You might also try to access negative number as an array index this also cause the runtime exception.

 Let me Explain With Example:

class ExceptionDemo
{
public static void main(String args[])
{
int Book[]=new int[5];
Book[5]=183;
System.out.printf("last element of Book=%d\n",Book[5]);
}


Output:

In the above error message indicate that the ArrayIndexOutOfBoundsException that has occured in the main() method of ExceptionDemo. Book array con take values 0 to 4 only but when we tried to access an element at index 5,which is out of bounds.

How to resolve ArrayIndexOutOfBoundsException:

By using Exception Handling feature of java to handle the exception using try and catch.The fallowing example shows how to handle ArrayIndexOutOfBoundsException using try and catch block

Example:

import java.io.Console;
class ExceptionDemo
{
public static void main(String args[])
{
Console con=System.console();
int Book[]=new int[5];
try{

Book[5]=183;
}
catch(ArrayIndexOutOfBoundsException e)
{
con.printf("ArrayIndexOutOfBound of Book array\n");
}
}

}

Output:








When you use multiple catch statements, it is important to remember that exception sub classes must come before any of their super class.

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...