Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ArrayIndexOutOfBoundsExceptionDemo {

private static Logger LOG = LoggerFactory.getLogger(ArrayIndexOutOfBoundsExceptionDemo.class);

public static void main(String[] args) {
int[] numbers = new int[] { 1, 2, 3, 4, 5 };

getArrayElementAtIndex(numbers, 5);
getListElementAtIndex(5);
addArrayElementsUsingLoop(numbers);
Expand All @@ -20,6 +25,21 @@ public static void addArrayElementsUsingLoop(int[] numbers) {
}
}

public static int addArrayElementsUsingLoopInsideTryCatchBlock(int[] numbers) {
int sum = 0;

try {
for (int i = 0; i <= numbers.length; i++) {
sum += numbers[i];
}
} catch (ArrayIndexOutOfBoundsException e) {
LOG.info("Attempted to access an index outside array bounds: {}", e.getMessage());
return -1;
}

return sum;
}

public static int getListElementAtIndex(int index) {
List<Integer> numbersList = Arrays.asList(1, 2, 3, 4, 5);
return numbersList.get(index);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.baeldung.exception.arrayindexoutofbounds;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.BeforeAll;
Expand All @@ -16,19 +17,23 @@ public static void setUp() {

@Test
void givenAnArrayOfSizeFive_whenAccessedElementBeyondRange_thenShouldThrowArrayIndexOutOfBoundsException() {
assertThrows(ArrayIndexOutOfBoundsException.class,
() -> ArrayIndexOutOfBoundsExceptionDemo.addArrayElementsUsingLoop(numbers));
assertThrows(ArrayIndexOutOfBoundsException.class, () -> ArrayIndexOutOfBoundsExceptionDemo.addArrayElementsUsingLoop(numbers));
}

@Test
void givenAnArrayOfSizeFive_whenAccessedElementBeyondRangeWithTryCatchBlock_thenShouldThrowArrayIndexOutOfBoundsException() {
int result = ArrayIndexOutOfBoundsExceptionDemo.addArrayElementsUsingLoopInsideTryCatchBlock(numbers);
assertEquals(-1, result);

}

@Test
void givenAnArrayOfSizeFive_whenAccessedAnElementAtIndexEqualToSize_thenShouldThrowArrayIndexOutOfBoundsException() {
assertThrows(ArrayIndexOutOfBoundsException.class,
() -> ArrayIndexOutOfBoundsExceptionDemo.getArrayElementAtIndex(numbers, 5));
assertThrows(ArrayIndexOutOfBoundsException.class, () -> ArrayIndexOutOfBoundsExceptionDemo.getArrayElementAtIndex(numbers, 5));
}

@Test
void givenAListReturnedByArraysAsListMethod_whenAccessedAnElementAtIndexEqualToSize_thenShouldThrowArrayIndexOutOfBoundsException() {
assertThrows(ArrayIndexOutOfBoundsException.class,
() -> ArrayIndexOutOfBoundsExceptionDemo.getListElementAtIndex(5));
assertThrows(ArrayIndexOutOfBoundsException.class, () -> ArrayIndexOutOfBoundsExceptionDemo.getListElementAtIndex(5));
}
}