Saturday, August 14, 2010

Try / catch: Birds.java


Courtesy of education.oracle.com

Given: 

1. class Birds {
2.   public static void main(String [] args) {
3.     try {
4.       throw new Exception();
5.     } catch (Exception e) {
6.       try {
7.         throw new Exception();
8.       } catch (Exception e2) { System.out.print("inner "); }
9.       System.out.print("middle ");
10.     }
11.     System.out.print("outer ");
12.   }
13. }

What is the result?
a) inner
b) inner outer
c) middle outer
d) inner middle outer
e) middle inner outer
f) Compilation fails.
g) An exception is thrown at runtime. 

d is correct. It is legal to nest try/catches and normal flow rules apply. 

No comments:

Post a Comment