• 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

need help with exception handling

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the correct answer is:13423, but why is not 134234? i still confuse with meth(1) call.Thanks
public class test{
public static void method(int i){
try {
if(i==1){
throw new Exception();
}
System.out.println("1");
}
catch(Exception e){
System.out.println("2");
return;
}
finally{
System.out.println("3");
}
System.out.println("4");
}
public static void main(String args[]){
method(0);
method(1);
}
}
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lees its because ur method (1) causes a exception and its handled by the catch , then as u might know the control is transferred to finally block before getting out of the program , and hence u get that 3 after which the program ends . hope this helps.
 
eric lee
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,vivek sivakumar
if catch by exception, than after finally statement,the rest of code will never execute, do i correct? Thanks.
try{
}catch{}
}finally{}
//Doing something here
i=i+1 <---never execute?
System.out.println <---never execute?
 
vivek sivakumar
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lee after the finally block is executed the control is tranferred to the first statement after finally block , so in any case your statement after finally operation will execute !!!
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by eric lee:
the correct answer is:13423, but why is not 134234? i still confuse with meth(1) call.Thanks
...
catch(Exception e){
System.out.println("2");
return;
}
finally{
System.out.println("3");
}
System.out.println("4");
}


Eric
The catch block contains a return statement. If an Exception is caught, the method will return after the finally block is executed. Thus 4 is not printed
 
eric lee
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,john &vivek.
 
Ew. You guys are ugly with a capital UG. Here, maybe this tiny ad can help:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic