This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
The finally code block is not necessarily the last code block to execute in a method; it is the last code block to execute as part of a try code block.
In the first code sample, there is no path of execution that allows the return statement after finally to execute. Either the try block code completes without throwing an exception and the return statement in the block is executed or an exception is thrown and caught, and the return statement in the catch block is executed.
What happens if you comment out the return in either the try or catch block?
Joined: Sep 20, 2004
wouldn't know, because I will have to construct a programm myself to get the code up and running .[The snippets are from a question bank.] since there' no full code up and running, and sticking to the 1st code block as-it-is, what were you trying to point me out if , say, I comment out the return in the catch block.?
if all the try and catch statements have return blocks the finally is unreachable.
I tested this code segment.
The finally block executes just before the return statement whether or not the exception is thrown.
The compiler error occurs because the last return statement, after the finally block, is never executed.
The reason the second code segment does compile is that the return statement was removed from the try block, so there is a way to execute the statement after the finally block. [ November 28, 2004: Message edited by: Mike Gershman ]
Howdy .. thanks to all for dwelling onto this one firstly.
In the mean time I did play around and have come to a conclusion: finally is always executed and there it returns, so there is no way that last return was going to get executed, it was toast. As 'J Borderi' mentioned juggling in 'n' out some returns from the code also helps, as long as the last 2 'returns' in the 'finally' don't go together. Thx. all