Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Try-with-resources scope of resource

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):

Content Assist suggests resource

Quick Fix suggests changing to resource variable, but this recursively produces the same problem it's trying to fix:

Redundant suggestion in Quick Fix

I would like to know what the correct scope limitation is, before raising a bug in the Eclipse Bug Tracker.

like image 480
ADTC Avatar asked Sep 09 '13 04:09

ADTC


People also ask

What is a try with resources?

The try -with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try -with-resources statement ensures that each resource is closed at the end of the statement.

Does try with resources Throw exception?

For try-with-resources, if an exception is thrown in a try block and in a try-with-resources statement, then the method returns the exception thrown in the try block. The exceptions thrown by try-with-resources are suppressed, i.e. we can say that try-with-resources block throws suppressed exceptions.

Can multiple resources be used in try with resource?

The ability to specify multiple resources in a single try-with-resources statement is a feature introduced in Java 7. But, it can be fraught with peril if not used carefully.

How try with resource works internally?

In the try-with-resources method, there is no use of the finally block. The file resource is opened in try block inside small brackets. Only the objects of those classes can be opened within the block which implements the AutoCloseable interface, and those objects should also be local.


2 Answers

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.

like image 149
kosa Avatar answered Oct 22 '22 11:10

kosa


Update from 2017 after Java 9 release

Now with Java 9 we have more syntactic sugar and we can have a resource declared outside the try-catch block but still handled properly. That's why with Java 9 the Try-With-Resources has been improved introducing a new syntax:

InputStream stream = new MyInputStream(...)
try (stream) {
   // do something with stream being sure that is going to be closed at the end
} catch(IOException e) {
   // you can surely use your resource here
}

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 and finally block

like image 7
rakwaht Avatar answered Oct 22 '22 10:10

rakwaht