In Java, the array index out-of-bounds exception is an exception that occurs when users try to access an array element using an invalid index. It helps to identify the specific line of code where the error occurred, making it easier to locate and fix the problem. Several benefits can be achieved by fixing the error, such as “correct program behavior”, “enhanced program stability”, “error handling and debugging” etc.
This article demonstrates the procedures to resolve the array index out-of-bounds exception.
How to Resolve the Array Index Out Of Bounds Exception in Java?
The purpose of fixing an error of array index out of bounds exception is to ensure that your program executes correctly and does not encounter runtime errors. Let’s explore the reasons with solutions:
Reason: Access an Invalid Index in an Array
The array index out-of-bounds exception is thrown to indicate that an array has been accessed with either a negative index or an index greater than or equal to the array’s length.
Visit the below code that displays the array index out of bound exception:
In the above code snippet:
- First, the class name “newClass” is created and inside its “main” function the “int” type array is created.
- Next, the array name is set to “arrayException” and its size is undefined/unlimited. And the numerical value from 1 to 9 is stored in this array.
- Then, the “for” loop is utilized that starts from “0” and ends at “8” which is the length of the “arrayException” variable.
- In each iteration, the value stored at each index in the “arrayException” array is displayed using the “System.out.println()” method.
After the end of the compilation phase:
The output displays that:
The error “ArrayIndexOutOfBoundsException” is generated by the compiler at runtime. This arises because the array length starts from “0” and the max index it has is “8”. However, the loop iterates till “9”.
Solution 1: Assign a Valid Range to Access an Index
To resolve this error, simply add “-1” next to “arrayException.length”. It makes the iteration till “8” times which is the max index of the array and hence no reason for the exception to be raised:
After updating the code, the output appears like this:
The output shows that the array elements are printed on the screen and the exception error is not generated.
Solution 2: Using “foreach” Loop
In Java, the “foreach” loop can be utilized for traversing and retrieving data from the array. With the help of the foreach loop the “Array Index Out of Bound Exception” error will not arise. Because it iterates for each array index, not till the specific length of the array.
Visit the below code, for a better understanding:
After the end of the compilation phase, the output appears like this:
The output illustrates that the exception error has been resolved.
Solution 3: Using Try Catch Block
The try-catch block is solely utilized for handling the exceptions that arise during the compilation of the program. It consists of two parts, the first one is “try” block, the code is inserted that may be the cause of the exception, and the second one deals with how to handle this exception.
Visit the below code in which the out-of-bound exception is being handled:
In the above code block:
- First, the class named “linuxhint” is created inside that array having a max index of “9” is created.
- Next, inside the “try” block “for” loop is utilized for retrieving the data from the array and then printing it on the console. The for loop iterates till the length of the array starts from the “0” Index.
- After that, the “catch” block is initialized by passing the “Exception” as a parameter in it.
- In the end, display the custom exception error message on the console.
After the end of the compilation phase, the output looks like this:
The output displays the custom exception displayed by the compiler.
Conclusion
The index out-of-bound exception occurs when the developer accesses an array element using an invalid index. It can be resolved by using a “foreach” loop or with the help of a try-catch block. The foreach loop retrieves the elements according to the elements available in the array by preventing the out-of-bound error. On the other hand, the try-and-catch block displays customized message errors in case of any exceptions.