Forums Register Login

What is the Overhead with finally block.

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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...)
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
<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
+Pie Number of slices to send: Send
"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
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
Bill,
My point exactly
+Pie Number of slices to send: Send
I know this is an old thread. Still:
Premature optimization is the root of all evil -- Knuth
Onion rings are vegetable donuts. Taste this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1483 times.
Similar Threads
insert query not executing..
singlethreadmodel
JDBC-Auto commit
how to close connection object in finally block when an exception is raised in try bl
Store Connection Pool
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:34:42.