Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What is the Overhead with finally block.  RSS feed

 
yogendra singh
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once i have been asked by some one why do u use finally block ?
i said if i want to execute some code irrespective excpton caught or not.
like close the connection..
and then he asked is this is good to put code in finally block ,doesnt it create some sort of overhead. if u awant to close the connection , u can put the line 'connection.close(); ' in end of try block and in the end of each catch block.
plz discuss it.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ask your friend why he thinks duplicate code is a good thing. Ask him which version, his or yours, would be easier to maintain when new catch clauses were added? Finally, ask him why there would be added overhead for a finally block, given that there's already an exception handler in place?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And even if there were noticable overhead in using a finally block (which I doubt), maintainability still overrides performance by default.
BTW, if you duplicate code, it will be less often executed, less often analyzed and optimized by the Hotspot engine - and therefore be slower! (Well, this is a painfully oversimplified view, of course, but might still be a good counterargument to your friend...)
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not only that, what does he do if an exception happens in your "catch" block before "close()" gets called? With a finally clause, you're guaranteed it will be called.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<rant>This preoccupation with overhead is a widespread evil that can lead to nothing but trouble. Having some idea about which operations are expensive and which are cheap is great but your primary focus should be on making code that is correct and clear so it can be easily maintained.
</rant>
Bill
 
Kirk Pepperdine
Author
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"This preoccupation with overhead is a widespread evil that can lead to nothing but trouble. Having some idea about which operations are expensive and which are cheap is great"
Humm, you seem to have contridicted yourself in a meaningful way
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there is a difference between being preoccupied with minutia - as evidenced in the question about using finally, and being able to recognize and avoid intrisically expensive operations. Maintaining clarity should take priority in any case.

Bill
[ January 19, 2004: Message edited by: William Brogden ]
 
Kirk Pepperdine
Author
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill,
My point exactly
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this is an old thread. Still:
Premature optimization is the root of all evil -- Knuth
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!