ArrayIndexOutOfBoundsException


    java.lang
Class ArrayIndexOutOfBoundsException
java.lang.Object
   java.lang.Throwable
       java.lang.Exception
           java.lang.RuntimeException
               java.lang.IndexOutOfBoundsException
                   java.lang.ArrayIndexOutOfBoundsException

An ArrayIndexOutOfBoundsException is thrown when an out-of-range index is detected by an array object. An out-of-range index occurs when the index is less than zero or greater than or equal to the size of the array.

    Synopsis
 
Class Name:
 
    java.lang.ArrayIndexOutOfBoundsException
 
Superclass:
 
    java.lang.IndexOutOfBoundsException

 Constructor Detail
           ArrayIndexOutOfBoundsException 
public ArrayIndexOutOfBoundsException()
 
Constructs an ArrayIndexOutOfBoundsException with no detail message.

          ArrayIndexOutOfBoundsException
 
public ArrayIndexOutOfBoundsException(int index)
 
Constructs a new ArrayIndexOutOfBoundsException class with an argument indicating the illegal index.
 
Parameters:
         index - the illegal index.

          ArrayIndexOutOfBoundsException
 
public ArrayIndexOutOfBoundsException(String s)
 
Constructs an ArrayIndexOutOfBoundsException class with the specified detail message.
 
Parameters:
         s - the detail message.
 
Example
 
In this example we are going to see how we can catch the exceptionArrayIndexOutOfBoundException. 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:
try{
        int a[] =new int[6];
                for(int i = 0; i<7; i++){
               
                 a[i]=i;
                }      
        }catch(Exception e){
               System.out.println("Exception:"+e);
                }

output:
Exception:java.lang.ArrayIndexOutOfBoundException:6

People who read this post also read :



1 comments:

ArrayIndexOutOfBoundsException is one of comman exceptions in java software development. ArrayIndexOutOfBoundsException is not checked exceptions and can not be declared in method signature

2 solutions for ArrayIndexOutOfBoundsException

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More