• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

skipping finally block

 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all

Is there any way to skip finally block with out terminating the program ?
 
Maneesh Godbole
Saloon Keeper
Posts: 11070
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12145
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15356
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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?
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 15356
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic