In the try
-with-resources construct of Java 7, I can declare a resource in the try
statement, and it will be closed automatically when it goes out of scope.
However, I don't find any indication of the scope of the resource made available. Specifically, is it available in the catch
/finally
blocks of the try block where it is declared?
I tried the following in Eclipse Kepler, but it's giving a mixed impression:
Resource variable is made available by Content Assist (Code Completion):
Quick Fix suggests changing to resource variable, but this recursively produces the same problem it's trying to fix:
I would like to know what the correct scope limitation is, before raising a bug in the Eclipse Bug Tracker.
This syntax is called Extended try-with-resources
As per JLS:
Will be translated to:
So, in your example, your resource will be limited to inner try block, so not available for outer
try/catch/finally
.EDIT:
By explicitly adding catch/finally block in your code, you are introducing nested try blocks.
Update from 2017 after Java 9 release
Now with
Java 9
we have more syntactic sugar and we can have a resource declared outside thetry-catch
block but still handled properly. That's why with Java 9 the Try-With-Resources has been improved introducing a new syntax:Note that this syntax will result in a compile time error for Java version 8 or minor
This is more "natural" way of writing even though in most use cases we don't need the resource outside the scope of the try block. The only restriction is that the reader variable should be effectively final or just final.
Anyway with this syntax you can surely have your resource used also in the
catch
andfinally
blockIn addition to @Nambari's answer:
That pretty much explains the behaviour, your resource goes out of scope in your explicit catch/finally block.
Reference
The correct scope limitation is within the declaration part
(...)
and the actualtry
block.The JLS states
So from the point it is declared in the ResourceSpecification
(...)
of thetry
onwards until the final closing}
bracket of thetry
Block
.