• 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

skipping finally block

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

Is there any way to skip finally block with out terminating the program ?

 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The finally block does not terminate the terminate the program unless you have explicitly called System.exit() inside it. When you want some code to execute, when after some exception is generated in the corresponding try block, you put that code in the finally block.

If you can tell us your problem in detail, we can offer some suggestions.
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If you can tell us your problem in detail, we can offer some suggestions.



I want to write code such that it should not execute finally block in any condition
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you include a finally block if you don't want the code to execute? It makes no sense to do this.
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It makes no sense to do this



Ya i know that but i'm asking is there any option to skip finally block
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

santhosh.R gowda wrote:

It makes no sense to do this



Ya i know that but i'm asking is there any option to skip finally block



No, you can't skip the finally block. But you can throw an exception in the first line of the finally block to prevent the rest from being executed.

You don't want to do that though. So for real, why do you ask? I am sure there is a better answer to solve your REAL problem, rather than to skip the finally block altogether.


<edit>
There are probably variations on this, like putting a return statement at the top of the finally block. All in all they would be equivalent to deleting the finally block altogether...
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The finally block is basically DEFINED as code that you want to always run. so, basically, you are asking

how do i not run code that i want to always run?

the obvious answer is that if you don't want it to always run, don't put it in the finally block. Nobody is forcing you to even HAVE a finally block. The question just doesn't make sense. That's why people are asking you what you REALLY want to do.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Luke wrote:So for real, why do you ask?


Maybe it was a "trick" question for a job interview?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The short answer is: you can't. The only way to skip the finally block is by terminating the JVM on the spot - using System.exit, crashing it (through some buggy native code), terminating it from outside (using task manager / kill) or by crashing the entire operating system itself.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:The short answer is: you can't. The only way to skip the finally block is by terminating the JVM on the spot - using System.exit, crashing it (through some buggy native code), terminating it from outside (using task manager / kill) or by crashing the entire operating system itself.

Ken Blair (in his 3rd post) in this old thread found another way of skipping the finally
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know another way!

If they switch on the LHC and it produces a black hole that swallows the Earth, and your computer falls into the black hole just at the moment before it's about to go to the finally-block, then it will be prevented from running!
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And if a ninja comes along and chops the entire machine into little pieces then the JVM will also exit, yes. I group all of these in "the OS crashes" though.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic