• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

unreachable code in 'finally'

 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.?
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic