How to handle a java.lang.IndexOutOfBoundsException in Java? Last Updated : 20 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In Java programming, IndexOutOfBoundsException is a runtime exception. It may occur when trying to access an index that is out of the bounds of an array. IndexOutOfBoundsException is defined as the RuntimeException. It can be used to find the out-of-bound run-time errors of an array. It is a part of the java.lang package. In this article, we will learn how to handle a java.lang.IndexOutOfBoundsException in Java. Syntax:try { // write code } catch (IndexOutOfBoundsException e) { // write code }Program to handle a java.lang.IndexOutOfBoundsException in JavaBelow is the Program to handle a java.lang.IndexOutOfBoundsException: Java // Java Program to handle a // java.lang.IndexOutOfBoundsException import java.util.Arrays; // Driver Class public class GfGIndexOutOfBoundsException { // Main method public static void main(String args[]) { // Array Intialization int[] array = {1, 2, 3}; try { // Attempt to access an element // outside the bounds of the array int value = array[3]; // throw IndexOutOfBoundsException System.out.println("Value: " + value); } catch (IndexOutOfBoundsException e) { // handle the exception and prints the message System.out.println("IndexOutOfBoundsException: " + e.getMessage()); } } } OutputIndexOutOfBoundsException: Index 3 out of bounds for length 3 Explanation of the Program:The above Java program is the example of the handle a java.lang.IndexOutOfBoundException. We already know this package handle the out of bound errors in the Java program. In this, we can initialize the 3 elements that means it can save index 0 to 2 into the array but within try block we are accessing the element in index 3.So, it's out of bound then try block can handle this error during runtime then catch takes control print the error. Comment More infoAdvertise with us Next Article How to handle a java.lang.IndexOutOfBoundsException in Java? K kadambalamatclo Follow Improve Article Tags : Java Java Programs Exception Handling Java-Exceptions Java-Exception Handling Java Examples +2 More Practice Tags : Java Similar Reads How to Handle an IOException in Java? An IOException in Java occurs when we try to perform some input or output tasks and then some issues occur. Programmers need to handle this issue explicitly with a piece of code that executes when an issue occurs. There is an entire class for handling such issues known as the IOException class, whic 3 min read How to Fix a java.lang.StringIndexOutOfBoundsException? In Java, String index out of bound is the common runtime exception that can occur when we try to access or manipulate the string using the invalid index. It can typically happen when the index provides is the negative, equal length of the string or greater than the length of the string. It can be ha 3 min read How to Handle SQLException in JDBC? Java Database Connectivity (JDBC) serves as the backbone for Java applications when interacting with databases. While establishing connections and executing queries, we developers often encounter SQLExceptions, which are inevitable in the real world. Handling those exceptions is crucial in the devel 4 min read How to Handle a java.lang.ArithmeticException in Java? In Java programming, the java.lang.ArithmeticException is an unchecked exception of arithmetic operations. This means you try to divisible by zero, which raises the runtime error. This error can be handled with the ArthmeticException. ArithmeticException can be defined as a runtime exception that ca 2 min read How to Solve java.lang.IllegalStateException in Java main Thread? An unexpected, unwanted event that disturbed the normal flow of a program is called Exception. Most of the time exception is caused by our program and these are recoverable. Example: If our program requirement is to read data from the remote file locating in U.S.A. At runtime, if a remote file is no 5 min read Like