Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes avoid finally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "avoid finally" Watch "avoid finally" New topic
Author

avoid finally

anarkali perera
Ranch Hand

Joined: Sep 10, 2009
Posts: 237
we know that finally block execute every time.But is there any other way to avoid the executing finally block without system.exit()?
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

yeah, do not write that block if you don't want to execute it. Or, create if statement that surrounds all the block:
anarkali perera
Ranch Hand

Joined: Sep 10, 2009
Posts: 237
no.this not i need.there is a way that is somthing automatically generated............
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
anarkali perera wrote:we know that finally block execute every time.But is there any other way to avoid the executing finally block without system.exit()?
Yes. It's called bad design. If you wrote a "finally" block you intend to execute it, so you don't want to avoid execution.

You will find this question comes up here repeatedly. Look what the late Stan James wrote here.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11441
    
  16

Cambell is right. the whole POINT of a finally block is that is it code that HAS to run, regardless of what else happens in the try/catch block. You are effectively asking "How do i not run the code I said I have to run"?

To that i'd reply "which is it? do you need to run it, or do you need to not run it?"

You should only put code that has to be run every time in the finally block. If you don't need to always run it, DON'T put it in there.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3350

Well said by Campbell and fred rosenberger!

It would be other way around called as 'Self Contradiction' Moreover, it would be a mere waste of all - including the lines in your java source code, compilers efforts to get the code compiled, etc., ...


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
But have you read what Stan wrote? That is such a beautiful quote
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3350

Yes of course. His verse is "Man, I hate when that happens"!

He was one such beautiful bartender. We miss him a lot!
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

God bless his soul.


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
 
Consider Paul's rocket mass heater.
 
subject: avoid finally