Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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

Exception

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test
{

public static String output="";
public static void foo(int i)
{
try{
if(i==1)
{
throw new Exception();
}

System.out.println(output +="1");

}

catch(Exception e)
{
System.out.println(output+="2");
return;
}
finally{
System.out.println(output+="3");
}
System.out.println(output+="4");
}


public static void main(String[]arf)
{
foo(0);
foo(1);
}
}

I thought the output will be 134234,but the output is 13423.
Why when the Exception is throw System.out.println(output+="4") not executed......can anybody explain this.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you have a "return" statement within the catch block. Take that out and you will get the missing 4 at the end.
 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Manoj...not working.Without return statement also the output is 13423
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


What makes you think you will get that output?
Your first run through throws no exception, so you see "1", then the finally block executes (as always), so you see "3", and then you see "4".
Your second run through throws an exception, so you see "2", then the finally block executes so you see "3", then you return.
Your output is "13423".

If you remove the return statement, the output is "134234".
 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks i got it...
[ February 06, 2005: Message edited by: ramya jp ]
 
If you are using a rototiller, you are doing it wrong. Even on 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