• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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
 
"How many licks ..." - I think all of this dog's research starts with these words. Tasty tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic