
- 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 - IndexOutOfBoundsException
The Java IndexOutOfBoundsException is thrown when an index is either less than zero or greater than the size of the array (or any other collection). The IndexOutOfBoundsException is a runtime exception that is thrown when you try to access an index that is either less than zero or greater than the size of the array (or any other collection).
Following is the reason when JVM throws an IndexOutOfBoundsException in Java:
- When the user tries to access an index that is either less than zero or greater than the size of the array (or any other collection), JVM throws an IndexOutOfBoundsException.
Constructors of IndexOutOfBoundsException
There are two constructors of IndexOutOfBoundsException class:
- IndexOutOfBoundsException(): This constructor is used to create an IndexOutOfBoundsException object without any message.
- IndexOutOfBoundsException(String message): This constructor is used to create an IndexOutOfBoundsException object with a message.
Methods of IndexOutOfBoundsException
There are some methods of IndexOutOfBoundsException 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 IndexOutOfBoundsException
In this example, we are trying to access an index that is greater than the size of the array, so JVM will throw an IndexOutOfBoundsException.
import java.util.ArrayList; public class IndexOutOfBoundsExceptionExample { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<>(); list.add(10); list.add(20); list.add(30); list.add(40); list.add(50); System.out.println(list.get(10)); } }
Output
Following is the output of the above code:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 10 out of bounds for length 5 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:100) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302) at java.base/java.util.Objects.checkIndex(Objects.java:385) at java.base/java.util.ArrayList.get(ArrayList.java:427) at IndexOutOfBoundsExceptionExample.main(IndexOutOfBoundsExceptionExample.java:12)
As you can see in the output, JVM throws an IndexOutOfBoundsException because we are trying to access an index that is greater than the size of the array.
Handling IndexOutOfBoundsException
In this example, we are trying to access an index that is greater than the size of the array, so JVM will throw an IndexOutOfBoundsException. We are handling this exception using try-catch block.
import java.util.ArrayList; public class IndexOutOfBoundsExceptionExample { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<>(); try { list.add(10); list.add(20); list.add(30); list.add(40); list.add(50); System.out.println(list.get(10)); } catch(IndexOutOfBoundsException e) { System.out.println("Index is out of bounds"); } } }
Output
Following is the output of the above code:
Index is out of bounds
As you can see in the output, we are handling the IndexOutOfBoundsException using try-catch block.
How to avoid IndexOutOfBoundsException?
There are some ways to avoid IndexOutOfBoundsException in Java:
- Always check the index before accessing any collection.
- Always use a loop to access any collection.
- Always use the size of the collection to access it.
By following the above ways, you can avoid the IndexOutOfBoundsException in Java.
Let's see an example to avoid IndexOutOfBoundsException:
import java.util.ArrayList; public class IndexOutOfBoundsExceptionExample { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<>(); list.add(10); list.add(20); list.add(30); list.add(40); list.add(50); for(int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } }
Output
Following is the output of the above code:
10 20 30 40 50
As you can see in the output, we are using the size of the collection to access it, so JVM does not throw an IndexOutOfBoundsException.
Let's see another example to avoid IndexOutOfBoundsException where we first check the index before accessing the collection:
import java.util.ArrayList; public class IndexOutOfBoundsExceptionExample { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<>(); list.add(10); list.add(20); list.add(30); list.add(40); list.add(50); if(list.size() > 10) { System.out.println(list.get(10)); }else { System.out.println("Index is out of bounds"); } } }
Output
Following is the output of the above code:
Index is out of bounds
We are checking the index before accessing the collection, so JVM does not throw an IndexOutOfBoundsException.