I had a question about the throws
keyword in the code below. In the method testLoop3() I am indicating that the IndexOutOfBoundsException
may be thrown with the throws keyword. But the method testLoop() is identical except it does not show that exception being thrown. What is the purpose of including throws IndexOutOfBoundsException if the exception is being sent to the calling method in both cases and the output is exactly the same? I read that the throws keyword is to make the caller of method aware of the exception and force the caller to handle it in the last line of the main() method it is not handling it.
package exceptions;
import java.util.ArrayList;
public class TestException {
public static void main(String[] args) {
System.out.println("------------------");
try {
testLoop();
} catch (Exception e) {
System.out.println("Exception from testLoop() passed to main= " + e);
}
System.out.println("After Exception");
System.out.println("------------------");
testLoop2();
try {
testLoop3();
} catch (Exception e) {
System.out.println("Exception from testLoop3() passed to main= " + e);
}
System.out.println("After Exception");
System.out.println("------------------");
testLoop();
System.out.println("------------------");
testLoop3();
}
private static void testLoop() {
System.out.println("In testLoop()");
ArrayList<Integer> arrLst = new ArrayList();
arrLst.add(4);
arrLst.add(6);
arrLst.add(2);
for(int x=0; x<5; x++) {
System.out.println(arrLst.get(x));
}
}
private static void testLoop2() {
System.out.println("In testLoop2()");
ArrayList<Integer> arrLst = new ArrayList();
arrLst.add(4);
arrLst.add(6);
arrLst.add(2);
try {
for(int x=0; x<5; x++) {
System.out.println(arrLst.get(x));
}
} catch (Exception e) {
System.out.println("Exception = " + e);
}
System.out.println("After Exception");
}
private static void testLoop3() throws IndexOutOfBoundsException{
System.out.println("In testLoop3()");
ArrayList<Integer> arrLst = new ArrayList();
arrLst.add(4);
arrLst.add(6);
arrLst.add(2);
for(int x=0; x<5; x++) {
System.out.println(arrLst.get(x));
}
}
}
Output:
------------------
In testLoop()
4
6
2
Exception from testLoop() passed to main= java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3
After Exception
------------------
In testLoop2()
4
6
2
Exception = java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3
After Exception
In testLoop3()
4
6
2
Exception from testLoop3() passed to main= java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3
After Exception
------------------
In testLoop()
4
6
2
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:459)
at exceptions.TestException.testLoop(TestException.java:42)
at exceptions.TestException.main(TestException.java:28)
I expected different results when calling testLoop() and testLoop3() due to the fact that testLoop3() has the thows IndexOutOfBoundsException and testLoop() does not. I excepted testLoop() not to propagate the exception to the main() method