• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

avoid finally

 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()?
 
Ranch Hand
Posts: 174
Java ME Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no.this not i need.there is a way that is somthing automatically generated............
 
Marshal
Posts: 76845
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13081
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 76845
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But have you read what Stan wrote? That is such a beautiful quote
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
God bless his soul.
 
You can't expect to wield supreme executive power just because
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic