How to handle a java.lang.IndexOutOfBoundsException in Java? Last Updated : 20 Feb, 2024 Comments Improve Suggest changes 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. Create Quiz Comment K kadambalamatclo Follow 0 Improve K kadambalamatclo Follow 0 Improve Article Tags : Java Java Programs Exception Handling Java-Exceptions Java-Exception Handling Java Examples +2 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like