Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

try/catch

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!help how the output is 4 and 5
public class jump{
public static void main(String[] args){
for(int i=0;i<10;++i){
try{
try{
if(i%3==0)
throw new Exception("e0");
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;
}
}
}
}
o/p is 4
5
please explain how is the o/p 4 and 5?

 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoa, this is one complicated loop! I made a few modifications to your code and it now looks like this:
public class jump{//1
public static void main(String[] args){//2
for(int i=0;i<10;i++){//3
try{//4
try{
if(i%3==0)
throw new Exception("e0");
System.out.println(i);
}//*4
catch(Exception inner){//5
i*=2;
System.out.println("1i="+i);
if(i%3==0)throw new Exception("e1");
}//*5
finally{//6
++i;
System.out.println("2i="+i);
}//*6
}//*4
catch(Exception outer){//7
i+=3;
System.out.println("3i="+i);
}//*7
finally{//8
--i;
System.out.println("4i="+i);
}//*8
}//*3
}//*2
}//*1
Try run it and figure out yourself.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nachiket,
Lets step through the code and see what is going on.
1. Start of for loop
i = 0
2. Inside 1st try block
3. Inside 2nd try block
4. 0 % 3 == 0 so throw new Exception( "e0" )
5. Catch the exception "e0" in the catch block of 2nd try block
set i = 0 * 0 = 0
i % 3 == 0 so throw new Exception( "e1" )
6. Before we leave 2nd try block execute finally block.
Set i = 0 + 1 = 1
7. Catch exception "e1" in catch statement of 1st try block
Set i = 1 + 3 = 4
8. Run finally block of 1st try block.
Set i = 4 - 1 = 3
9. Back at for loop top.
Pass loop quit condition
Increment i: i = 3 + 1 = 4
10. Inside 1st try block
11. Inside 2nd try block
12. 4 % 3 != 0 so print out 4
13. Before we end 2nd try block execute finally block
Set i = 4 + 1 = 5
14. Run finally block of 1st try block
Set i = 5 - 1 = 4
15. Back at for loop top
Pass loop condition
Increment i = 4 + 1 = 5
16. Inside 1st try block
17. Inside 2nd try block
18. 5 % 3 != 0 so print out 5
19. Before we end 2nd try block execute finally block
Set i = 5 + 1 = 6
20. Run finally block of 1st try block
Set i = 6 - 1 = 5
21. Back at for loop top
Pass loop condition
Increment i: i = 5 + 1 = 6
22. Inside 1st try block
23. Inside 2nd try block
24. 6 % 3 == 0 so throw new Exception( "e0" )
25. Catch exception "e0" inside 2nd try/catch block
Set i = 6 * 2 = 12
12 % 3 == 0 so throw new Exception( "e1" )
26. Before we quit execute the 2nd finally block
Increment i. i = 12 + 1 = 13
27. Catch exception "e1" inside 1st try/catch block
Set i = 13 + 3 = 16
28. Execute 1st finally block
Increment i. i = 16 + 1 = 17
29. Back at for loop start
Don't pass loop condition
30. Done!
Whew!
Manfred.
 
nachiket deshpande
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Manfred!!
 
On top of spaghetti all covered in cheese, there was this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic