• Post Reply Bookmark Topic Watch Topic
  • New Topic

can execution of only finally block be stoped anyway  RSS feed

 
Gajendra Kangokar
Ranch Hand
Posts: 94
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

Is there any way I can stop execution of finally block?
System.exit(0); halts JVM i dont want to use this, without using System.exit(0) is there any other possible way?

Thanks
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Short answer: No.
Long answer: Why would you want to stop the finally being executed? What problem are you trying to solve?
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all .. what is the practical business scenario behind this requirement ? OR is this just the push your limit kind of question ?
consider a scenario where as soon as JVM reaches finally block, out of memory error occurred , in this scenario, finally block shall not get executed as whole application has come down. In general, if any Error occured just before finally block, finally block shall not get executed.
Apart from this and System.exit, finally block shall definitely get executed if present in code as per JVM specifications

 
Gajendra Kangokar
Ranch Hand
Posts: 94
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was an interview question asked recently.
Thanks for the explanation.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's a real business requirement and not language lawyer speculations:


 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Paul C's suggestion does is convert the finally to an empty block which still runs. You can prevent the finally by ensuring the try or catch never complete (e.g. with an infinite loop). Or you can pray for a meteorite strike.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!