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(); }
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.
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.
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.
You can use catch and finally blocks with try-with-resources statement just like an ordinary try statement.
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.
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.
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With