File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes skipping finally block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "skipping finally block" Watch "skipping finally block" New topic
Author

skipping finally block

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Dear all

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


Creativity is nothing but Breaking Rules
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
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

Joined: Apr 14, 2004
Posts: 10336

Why would you include a finally block if you don't want the code to execute? It makes no sense to do this.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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...


Steve
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

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

Maybe it was a "trick" question for a job interview?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
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

Joined: Aug 16, 2005
Posts: 14268
    
  21

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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: skipping finally block