What is printed for each of the four runs?
d caught Ex1
c caught Ex2
b caught Ex1
b caught Ex3
a caught Ex4
execution stops due to uncaught exception Ex1 thrown in main
Question 1:
static void g() throws Ex1, Ex3 { try { f(); } catch (Ex1 ex1) { System.out.println("Ex1 caught"); } catch (Ex2 ex2) { System.out.println("Ex2 caught"); throw new Ex1(); } }
Question 2:
Part A:
f(0, X, "hi")
nothing printed
an uncaught ArithmeticException is thrown
f(10, X, "")
prints "in finally clause"
an uncaught StringIndexOutOfBoundsException is thrown
f(10, X, "bye")
prints "array error", "in finally clause"
an uncaught InternalError is thrown
f(10, X, null)
prints "null ptr", "in finally clause", "after try block"
Part B: Function f doesn't need to have a throws clause that lists the uncaught exceptions that it might throw because only uncaught CHECKED exceptions need to be listed in a method's throws clause. The uncaught exceptions that f might throw are all UNCHECKED exceptions.