Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

avoid finally

 
anarkali perera
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
Java Java ME Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no.this not i need.there is a way that is somthing automatically generated............
 
Campbell Ritchie
Sheriff
Pie
Posts: 49785
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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., ...
 
Campbell Ritchie
Sheriff
Pie
Posts: 49785
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But have you read what Stan wrote? That is such a beautiful quote
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1306
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
God bless his soul.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic