Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Try-With Resource when AutoCloseable is null

How does the try-with feature work for AutoCloseable variables that have been declared null?

I assumed this would lead to a null pointer exception when it attempts to invoke close on the variable, but it runs no problem:

try (BufferedReader br = null){     System.out.println("Test"); } catch (IOException e){     e.printStackTrace(); } 
like image 753
flakes Avatar asked Feb 12 '16 21:02

flakes


People also ask

Can we use try with resources without catch?

Yes, It is possible to have a try block without a catch block by using a final block. As we know, a final block will always execute even there is an exception occurred in a try block, except System.

Does Java 8 support try resources?

The Stream interface extends the java. lang. AutoCloseable interface. So if you want to correctly close your streams, you have to use try with resources.

How do you handle exceptions in trying with resources?

If an exception is thrown from within a Java try-with-resources block, any resource opened inside the parentheses of the try block will still get closed automatically. The throwing of the exception will force the execution to leave the try block, and this will force the automatic closing of the resource.

Can we use finally with try with resources?

You can use catch and finally blocks with try-with-resources statement just like an ordinary try statement.

How to auto-close a try in TRY– CATCH-FINALLY block?

Simply put, to be auto-closed, a resource has to be both declared and initialized inside the try: 3. Replacing try–catch-finally With try-with-resources The simple and obvious way to use the new try-with-resources functionality is to replace the traditional and verbose try-catch-finally block. Let's compare the following code samples.

How do you close a custom resource in try with resources?

A Custom Resource With AutoCloseable To construct a custom resource that will be correctly handled by a try-with-resources block, the class should implement the Closeable or AutoCloseable interfaces and override the close method: 6. Resource Closing Order Resources that were defined/acquired first will be closed last.

Which objects can be passed to AutoCloseable in Java?

You can pass any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable. The following example writes a string into a file. It uses an instance of FileOutputStream to write data into the file. FileOutputStream is a resource that must be closed after the program is finished with it.

How to declare multiple resources in a try-with-resources block?

We can declare multiple resources just fine in a try-with-resources block by separating them with a semicolon: 5. A Custom Resource With AutoCloseable To construct a custom resource that will be correctly handled by a try-with-resources block, the class should implement the Closeable or AutoCloseable interfaces and override the close method:


1 Answers

The Java Language Specification specifies that it is closed only if non-null, in section 14.20.3. try-with-resources:

A resource is closed only if it initialized to a non-null value.

This can actually be useful, when a resource might present sometimes, and absent others.

For example, say you might or might not have a closeable proxy to some remote logging system.

try ( IRemoteLogger remoteLogger = getRemoteLoggerMaybe() ) {     if ( null != remoteLogger ) {        ...     } } 

If the reference is non-null, the remote logger proxy is closed, as we expect. But if the reference is null, no attempt is made to call close() on it, no NullPointerException is thrown, and the code still works.

like image 181
Andy Thomas Avatar answered Sep 20 '22 14:09

Andy Thomas