Nested try statements

From: Thomas W. Gauperaa (thomas.gauperaa_at_chelloneitakkdu.no)
Date: 10/31/03


Date: Fri, 31 Oct 2003 13:49:56 +0100

I'm a bit puzzled by this exam question (java programmer certification)
which has nested try statements. The correct answer is an output of values 5
and 8. I thought the first try statement which results in throwing an E0
exception would make the program jump directly to "catch (exception outer)"
and thereby bypassing the inner try statement and its system.out.println
statement? or will it do the inner try statement first?

Thanks!

Here it is:

class Question {
 public static void main(String[] args) {
  for(int i=0;i<10;++i) {
   try {
    if(i % 3 == 0) throw new Exception("E0");
    try {
     if(i % 3 == 1) throw new Exception("E1");
     System.out.println(i);
    }catch (Exception inner) {
      i*=2;
     }finally{
      ++i;
     }
    }catch (Exception outer) {
     i+=3;
    }finally{
     ++i;
  }
 }
}