Issue
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.
Solution
This syntax is called Extended try-with-resources
As per JLS:
try ResourceSpecification
Block
Catchesopt
Finallyopt
Will be translated to:
try {
try ResourceSpecification
Block
}
Catchesopt
Finallyopt
So, in your example, your resource will be limited to inner try block, so not available for outer try/catch/finally
.
EDIT:
my question does not have nested try blocks
By explicitly adding catch/finally block in your code, you are introducing nested try blocks.
Answered By - kosa
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.