• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Order of execution

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai All,

class Question{
public static void main(String args[]){
for(int i=0;i<10;i++){
try{
try{
if(i % 3 == 0)
throw new Exception("EO");
System.out.println(i);
} catch (Exception inner){
i *= 2;
if(i % 3 == 0)
throw new Exception("E1");
} finally{
++i;
}
}catch(Exception outer ){
i+= 3;
}finally{
--i;
}
}
}
}
Can anyone tell me how the above code is executed(I mean the order of execution and the output).

Thanks
sudha
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,sudha...
i also confused with the two-try-block code.however...i add some
code into urs and run again,then i found the order of the execution.
the first loop
1.i=0; the "if(i%3==0)" throw a new exception "EO".
2.do the code "i*=2" in inner catch block i=0 and also trow a new exception "E1"
3.go on, the inner finally block is working,i=1 this time
4.turn to the outer catch and finally block and at last i=3
5.execute the "i++" code in the "for" statement,i=4
the second loop
1.inner try "(i%3==0)" results false,the println() is called and print the first number 4;
2.inner finally block-> outer finally block then-> i++, i=5;
the third loop
1.inner try also print,the second number 5 is printed
2......i=6;
the fourth loop
1.....
2.....
(i*=2; i turns to 12 and the followed code all increase its value,i is more than 10.execution stops here)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic