• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

doubt in finally blk execution

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

finally will always be executed only exception is that when any of the catch blocks or try block calls system.exit() but consider this code.

public class testfinally{

void test(int i){

if ( i == 1)
return; // replace this with throw new RuntimeException();

try{
System.out.println("1");
}catch(Exception e){e.printStackTrace();}
finally{System.out.println("in finally");}

}

public static void main(String[] args){

new testfianlly().test(1);
}

}

even in this case when the if condition is satisfied it simply returns or throw some exception in any of this cases to finally block doesnt execute what is the reason for finally block not being executed.

Thanks
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the throw new RuntimeException() is outside of the try block.

Finally will always execute even if an exception is thrown in the try block. In this case exception is outside. So programs stops before it reaches try.
 
vishwa raj
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so it means to say that finally is guranteed to execute only when the control is in the context of try,catch if the control is much before then finally will not execute.

Thanks
 
vishwa raj
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is my understanding correct ? Please correct me if i am wrong.

Thanks
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic