IndexOutOfBoundsException in java

In this exception and exception handling tutorial we will learn what is IndexOutOfBoundsException in java, hierarchy of java.lang.IndexOutOfBoundsException, two most frequently occurring subclasses of IndexOutOfBoundsException in java.




What is hierarchy of java.lang.IndexOutOfBoundsException?

-java.lang.Object
-java.lang.Throwable
 -java.lang.Exception
  -java.lang.RuntimeException
   -java.lang.IndexOutOfBoundsException

IndexOutOfBoundsException is Checked (compile time exceptions) and UnChecked (RuntimeExceptions) in java ?

java.lang.IndexOutOfBoundsException is a RuntimeExceptions in java.


What is IndexOutOfBoundsException in java?

IndexOutOfBoundsException is thrown when an array, String or may be some other type which allows indexed access is accessed with an illegal index.

The index accessed may be >

  • Negative or
  • equal to the size of array
  • greater than the size of array.


There are two very frequently occurring subclasses of IndexOutOfBoundsException >
  1. ArrayIndexOutOfBoundsException

  2. StringIndexOutOfBoundsException




What is hierarchy of java.lang.ArrayIndexOutOfBoundsException?
-java.lang.Object
-java.lang.Throwable
 -java.lang.Exception
  -java.lang.RuntimeException
   -java.lang.IndexOutOfBoundsException
    -java.lang.ArrayIndexOutOfBoundsException

ArrayIndexOutOfBoundsException is Checked (compile time exceptions) and UnChecked (RuntimeExceptions) in java ?

java.lang.ArrayIndexOutOfBoundsException is a RuntimeExceptions in java.


What is ArrayIndexOutOfBoundsException in java?

ArrayIndexOutOfBoundsException is thrown when an array is accessed with an illegal index.

The index accessed may be >

  • Negative or
  • equal to the size of array
  • greater than the size of array.



Scenarios where ArrayIndexOutOfBoundsException may be thrown in java>



Let’s take an example where using array can throw ArrayIndexOutOfBoundsException in java >

  1. If index accessed is Negative, ArrayIndexOutOfBoundsException is thrown
  2. If index accessed is equal to the size of array, ArrayIndexOutOfBoundsException is thrown
  3. If index accessed is greater than the size of array, ArrayIndexOutOfBoundsException is thrown

public class ArrayIndexOutOfBoundsExceptionExample {
public static void main(String args[]) {
     int ar[] = { 11, 22, 23, 31};
    
     //If index accessed is Negative, ArrayIndexOutOfBoundsException is thrown
     //System.out.println(ar[-1]);
     System.out.println(ar[0]); //11
     System.out.println(ar[1]); //22
     System.out.println(ar[2]); //23
     System.out.println(ar[3]); //31
    
     //If index accessed is equal to the size of array, ArrayIndexOutOfBoundsException is thrown
     //System.out.println(ar[4]);
    
     //If index accessed is greater than the size of array, ArrayIndexOutOfBoundsException is thrown
     //System.out.println(ar[5]);
}
}

If we uncomment any of the above comment line ArrayIndexOutOfBoundsException will be thrown >

System.out.println(ar[-1]);
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at ArrayIndexOutOfBoundsExceptionExample.main( ArrayIndexOutOfBoundsExceptionExample.java:6)


System.out.println(ar[4]);
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at ArrayIndexOutOfBoundsExceptionExample.main( ArrayIndexOutOfBoundsExceptionExample.java:14)


System.out.println(ar[5]);
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at ArrayIndexOutOfBoundsExceptionExample.main( ArrayIndexOutOfBoundsExceptionExample.java:17)





Let’s take an example where using java.util.ArrayList can throw ArrayIndexOutOfBoundsException in java >

  1. If index accessed is Negative, ArrayIndexOutOfBoundsException is thrown
  2. If index accessed is equal to the size of ArrayList, ArrayIndexOutOfBoundsException is thrown
  3. If index accessed is greater than the size of ArrayList, ArrayIndexOutOfBoundsException is thrown

import java.util.ArrayList;
import java.util.List;
public class ArrayIndexOutOfBoundsExceptionExample2 {
public static void main(String args[]) {
    
     List<Integer> arrayList = new ArrayList<Integer>();
     arrayList.add(11);
     arrayList.add(22);
     arrayList.add(23);
     arrayList.add(31);
    
     //If index accessed is Negative, ArrayIndexOutOfBoundsException is thrown
     //System.out.println(arrayList.get(-1));
     System.out.println(arrayList.get(0)); //11
     System.out.println(arrayList.get(1)); //22
     System.out.println(arrayList.get(2)); //23
     System.out.println(arrayList.get(3)); //31
    
     //If index accessed is equal to the size of array, ArrayIndexOutOfBoundsException is thrown
     //System.out.println(arrayList.get(4));
    
     //If index accessed is greater than the size of array, ArrayIndexOutOfBoundsException is thrown
     //System.out.println(arrayList.get(5));
}
}
/*OUTPUT
11
22
23
31
*/

If we uncomment any of the above comment line ArrayIndexOutOfBoundsException will be thrown >

System.out.println(arrayList.get(-1))
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at ArrayIndexOutOfBoundsExceptionExample2.main(ArrayIndexOutOfBoundsExceptionExample2.java:14)



System.out.println(arrayList.get(4))
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at ArrayIndexOutOfBoundsExceptionExample2.main(ArrayIndexOutOfBoundsExceptionExample2.java:22)



System.out.println(arrayList.get(5))
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 4
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at ArrayIndexOutOfBoundsExceptionExample2.main(ArrayIndexOutOfBoundsExceptionExample2.java:25)





2. StringIndexOutOfBoundsException


What is hierarchy of java.lang.StringIndexOutOfBoundsException?

-java.lang.Object
-java.lang.Throwable
 -java.lang.Exception
  -java.lang.RuntimeException
   -java.lang.IndexOutOfBoundsException
    -java.lang.StringIndexOutOfBoundsException


StringIndexOutOfBoundsException is Checked (compile time exceptions) and UnChecked (RuntimeExceptions) in java ?

java.lang.StringIndexOutOfBoundsException is a RuntimeExceptions in java.


What is StringIndexOutOfBoundsException in java?

StringIndexOutOfBoundsException is thrown when an string is accessed with an illegal index.

The index accessed may be >

  • Negative or
  • equal to the size of string
  • greater than the size of array.



Scenarios where StringIndexOutOfBoundsException may be thrown in java>

  1. If index accessed is Negative, StringIndexOutOfBoundsException is thrown
  2. If index accessed is equal to the size of array, StringIndexOutOfBoundsException is thrown
  3. If index accessed is greater than the size of array, StringIndexOutOfBoundsException is thrown

public class StringIndexOutOfBoundsExceptionExample {
public static void main(String args[]) {
     String str= "abcd";
    
     //If index accessed is Negative, StringIndexOutOfBoundsException is thrown
     //System.out.println(str.charAt(-1));
     System.out.println(str.charAt(0)); //a
     System.out.println(str.charAt(1)); //b
     System.out.println(str.charAt(2)); //c
     System.out.println(str.charAt(3)); //d
    
     //If index accessed is equal to the size of array, StringIndexOutOfBoundsException is thrown
     //System.out.println(str.charAt(4));
    
     //If index accessed is greater than the size of array, StringIndexOutOfBoundsException is thrown
     //System.out.println(str.charAt(5));
}
}


If we uncomment any of the above comment line StringIndexOutOfBoundsException will be thrown >

System.out.println(str.charAt(-1));
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.charAt(Unknown Source)
at  StringIndexOutOfBoundsExceptionExample.main(StringIndexOutOfBoundsExceptionExample.java:7)



System.out.println(str.charAt(4));
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.lang.String.charAt(Unknown Source)
at  StringIndexOutOfBoundsExceptionExample.main(StringIndexOutOfBoundsExceptionExample.java:14)



System.out.println(str.charAt(5));
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
at java.lang.String.charAt(Unknown Source)
at  StringIndexOutOfBoundsExceptionExample.main(StringIndexOutOfBoundsExceptionExample.java:17)


So in this exception and exception handling tutorial we learned what is IndexOutOfBoundsException in java, hierarchy of java.lang.IndexOutOfBoundsException, two most frequently occurring subclasses of IndexOutOfBoundsException in java i.e. ArrayIndexOutOfBoundsException and StringIndexOutOfBoundsException  in java.


eEdit
Must read for you :