Skip to content

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

core-java-modules/core-java-exceptions-4/src/main/java/com/baeldung/exception/arrayindexoutofbounds/ArrayIndexOutOfBoundsExceptionDemo.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
import java.util.Arrays;
44
import java.util.List;
55

6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
69
public class ArrayIndexOutOfBoundsExceptionDemo {
710

11+
private static Logger LOG = LoggerFactory.getLogger(ArrayIndexOutOfBoundsExceptionDemo.class);
12+
813
public static void main(String[] args) {
914
int[] numbers = new int[] { 1, 2, 3, 4, 5 };
10-
15+
1116
getArrayElementAtIndex(numbers, 5);
1217
getListElementAtIndex(5);
1318
addArrayElementsUsingLoop(numbers);
@@ -20,6 +25,21 @@ public static void addArrayElementsUsingLoop(int[] numbers) {
2025
}
2126
}
2227

28+
public static int addArrayElementsUsingLoopInsideTryCatchBlock(int[] numbers) {
29+
int sum = 0;
30+
31+
try {
32+
for (int i = 0; i <= numbers.length; i++) {
33+
sum += numbers[i];
34+
}
35+
} catch (ArrayIndexOutOfBoundsException e) {
36+
LOG.info("Attempted to access an index outside array bounds: {}", e.getMessage());
37+
return -1;
38+
}
39+
40+
return sum;
41+
}
42+
2343
public static int getListElementAtIndex(int index) {
2444
List<Integer> numbersList = Arrays.asList(1, 2, 3, 4, 5);
2545
return numbersList.get(index);

core-java-modules/core-java-exceptions-4/src/test/java/com/baeldung/exception/arrayindexoutofbounds/ArrayIndexOutOfBoundsExceptionDemoUnitTest.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.baeldung.exception.arrayindexoutofbounds;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
34
import static org.junit.jupiter.api.Assertions.assertThrows;
45

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

1718
@Test
1819
void givenAnArrayOfSizeFive_whenAccessedElementBeyondRange_thenShouldThrowArrayIndexOutOfBoundsException() {
19-
assertThrows(ArrayIndexOutOfBoundsException.class,
20-
() -> ArrayIndexOutOfBoundsExceptionDemo.addArrayElementsUsingLoop(numbers));
20+
assertThrows(ArrayIndexOutOfBoundsException.class, () -> ArrayIndexOutOfBoundsExceptionDemo.addArrayElementsUsingLoop(numbers));
21+
}
22+
23+
@Test
24+
void givenAnArrayOfSizeFive_whenAccessedElementBeyondRangeWithTryCatchBlock_thenShouldThrowArrayIndexOutOfBoundsException() {
25+
int result = ArrayIndexOutOfBoundsExceptionDemo.addArrayElementsUsingLoopInsideTryCatchBlock(numbers);
26+
assertEquals(-1, result);
27+
2128
}
2229

2330
@Test
2431
void givenAnArrayOfSizeFive_whenAccessedAnElementAtIndexEqualToSize_thenShouldThrowArrayIndexOutOfBoundsException() {
25-
assertThrows(ArrayIndexOutOfBoundsException.class,
26-
() -> ArrayIndexOutOfBoundsExceptionDemo.getArrayElementAtIndex(numbers, 5));
32+
assertThrows(ArrayIndexOutOfBoundsException.class, () -> ArrayIndexOutOfBoundsExceptionDemo.getArrayElementAtIndex(numbers, 5));
2733
}
2834

2935
@Test
3036
void givenAListReturnedByArraysAsListMethod_whenAccessedAnElementAtIndexEqualToSize_thenShouldThrowArrayIndexOutOfBoundsException() {
31-
assertThrows(ArrayIndexOutOfBoundsException.class,
32-
() -> ArrayIndexOutOfBoundsExceptionDemo.getListElementAtIndex(5));
37+
assertThrows(ArrayIndexOutOfBoundsException.class, () -> ArrayIndexOutOfBoundsExceptionDemo.getListElementAtIndex(5));
3338
}
3439
}

0 commit comments

Comments
 (0)