• 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

question on try catch block

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the following code
public class test{
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("EI");
}
finally{
++i;
}
}catch(Exception outer){
i+=3;
}finally{
--i;
}
}
}
}
can anyone explain how i get the output as 4 5
thankx in advance
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is that for loop executes only for the values 0,4,5 and 6. When the loop executes for the 1st time it throws the exceptions and the value of i becomes 4 next time it enters into the for loop again.
Actually the body of the loop changes the value of i from 0 to 3, and the increment statement of the for loop makes it 4.
4 and 5 are printed and when i becomes 6 exceptions are thrown again and the value of i becomes greater than 10, there by terminating the loop.
Bibhuti Dutta
 
poornima viswa
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you i understood the flow now
I was just missing the increment that takes place in the for loop
 
reply
    Bookmark Topic Watch Topic
  • New Topic