Index Out Of BoundsIndex Out Of Bounds

What is an Index Out of Bounds Error?

An "Index Out of Bounds" (IOB) exception occurs in Java when you attempt to access or modify an element in an array using an index that falls outside the valid range of indices for that array. In Java, arrays are zero-indexed, which means the first element has an index of 0, the second element has an index of 1, and so forth.

When you try to access an element with an index less than 0 or greater than or equal to the array's length, the "index out of bounds" exception is thrown. This exception serves as a way for the Java program to indicate that you are attempting an invalid operation on the array.


In Java, there are numerous exceptions. We will explore the exception hierarchy, learn how to create custom exceptions, and handle them correctly in a separate course.

There is an example that will throw an Index out of bounds exception:


To address the "index out of bounds" exception, you can follow these steps:

  • Ensure that the index you use to access the array falls within the valid range of indices.
  • Verify that the array is not empty before attempting to access any elements.
  • Review your program's logic to confirm the accuracy of index calculations.
  • Employ conditional statements or loops to prevent accessing elements beyond the valid index range.

Here's an example that demonstrates how to handle the index out of bounds exception:


In this example, we validate whether the index falls within the valid range before attempting to access the array. If the index is valid, we retrieve the element at that position. Otherwise, we manage the exception by displaying an error message.

1. When the ``Index out of bounds exception`` occurs?
2. How to solve ``IOB``?(select multiple answer choices)


When the Index out of bounds exception occurs?

Select the correct answer


How to solve IOB?(select multiple answer choices)

Select a few correct answers

Everything was clear?

Section 4. Chapter 3