File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes unreachable code in 'finally' Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "unreachable code in Watch "unreachable code in New topic
Author

unreachable code in 'finally'

Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131



the above code won't compile because the return statement after 'finally' is unreachable. Fine.

but the "System.out.println("5");"[below] IS reachable after the 'finally' from the following code.




so what benchmark do I need to apply, regarding what's reachable and unreachable after the 'finally' clause.
Is it "any return statements " are unreachable ?

TIA
Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151
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?
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
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.?
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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 ]

Mike Gershman
SCJP 1.4, SCWCD in process
Inuka Vincit
Ranch Hand

Joined: Aug 10, 2004
Posts: 175
Originally posted by Mike Gershman:

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 ]


wow I missed that one, I was thinking it didnt sound logical in the first place. deleted my long ass useless post


MCP (C# application dev 70-316) 860<br />SCJP 1.4 100% SCJD (URLyBird) 378<br />MAD 100% nuts
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: unreachable code in 'finally'
 
Similar Threads
how the compiler decides about the unreachable stmt
Find the unreachable statement in this code
JQ+ and confusion with try/catch/finally
COnfusion About Unreachable Code
return