Given:
public class Main { public static void checkIt(int a) { if (a == 1) throw new IllegalArgumentException(); }/* www . j a va2 s .com*/ public static void main(String[] args) { for (int x = 0; x < 2; x++) try { System.out.print("t "); checkIt(x); System.out.print("Here "); } finally { System.out.print("f "); } } }
What is the result?
E is correct.
As far as the exception goes, it's thrown during the second iteration of the for loop, and it's uncaught and undeclared (which is legal since it's a runtime exception).
Remember, finally (almost) ALWAYS runs.
In main()
, the code is legal: the entire try-finally code is considered a single statement from the for loop's perspective, and of course the idea of a try-finally is legal; a catch statement is not required.