
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - ArrayIndexOutOfBoundsException
The Java ArrayIndexOutOfBoundsException is thrown when an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
In Java, ArrayIndexOutOfBoundsException is present in Java.lang package and the base class of this exception is java.lang.ArrayIndexOutOfBoundsException. JVM raises an ArrayIndexOutOfBoundsException when the user tries to access an array with an illegal index.
There are some scenarios when JVM throws an ArrayIndexOutOfBoundsException in Java, two of them are following:
- When the user tries to access an array with an illegal index, JVM throws an ArrayIndexOutOfBoundsException.
- When the user tries to access an array with a negative index, JVM throws an ArrayIndexOutOfBoundsException.
Constructors of ArrayIndexOutOfBoundsException
There are two constructors of ArrayIndexOutOfBoundsException class:
- ArrayIndexOutOfBoundsException(): This constructor is used to create an ArrayIndexOutOfBoundsException object without any message.
- ArrayIndexOutOfBoundsException(String message): This constructor is used to create an ArrayIndexOutOfBoundsException object with a message.
Methods of ArrayIndexOutOfBoundsException
There are some methods of ArrayIndexOutOfBoundsException class:
Method | Description |
---|---|
getMessage() | It is used to return the message of the exception. |
toString() | It is used to return the detail message string of the exception. |
printStackTrace() | It is used to print the stack trace of the exception. |
Example of ArrayIndexOutOfBoundsException
In this example, we are trying to access an array with an illegal index, so JVM will throw an ArrayIndexOutOfBoundsException.
public class ArrayIndexOutOfBoundsExceptionExample { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; System.out.println(arr[10]); } }
Output
Following is the output of the above code:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at ArrayIndexOutOfBoundsExceptionExample.main(ArrayIndexOutOfBoundsExceptionExample.java:4)
As you can see in the output, JVM throws an ArrayIndexOutOfBoundsException because we are trying to access an array with an illegal index.
Handling ArrayIndexOutOfBoundsException
In this example, we are trying to access an array with an illegal index, so JVM will throw an ArrayIndexOutOfBoundsException. We are handling this exception using try-catch block.
public class ArrayIndexOutOfBoundsExceptionExample { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; try { System.out.println(arr[10]); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Array index is out of bounds"); } } }
Output
Following is the output of the above code:
Array index is out of bounds
How to avoid ArrayIndexOutOfBoundsException?
There are some ways to avoid ArrayIndexOutOfBoundsException in Java:
- Always check the index before accessing an array.
- Always use a loop to access an array.
- Always use the length of the array to access the array.
By following the above ways, you can avoid ArrayIndexOutOfBoundsException in Java.
Let's see an example to avoid ArrayIndexOutOfBoundsException:
public class ArrayIndexOutOfBoundsExceptionExample { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; for(int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } }
Output
Following is the output of the above code:
1 2 3 4 5
As you can see in the output, we are accessing the array using a loop, so JVM will not throw an ArrayIndexOutOfBoundsException.