What is the new concept of Autocloseable in try block in jdk 1.7. Is this really required or just to enhance something in try catch block to make it more advance.
-
2http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html – shmosel Jan 08 '15 at 08:21
3 Answers
It enables the use of try-with-resources
which is a new feature from Java 7.
Old-school:
InputStream is = null;
try {
is = ...;
// do stuff with is...
} catch (IOException e) {
// handle exception
} finally {
if (is != null) {
try {
is.close();
} catch (IOException innerE) {
// Handle exception
}
}
}
New-school:
try (InputStream is = ...) {
// do stuff with is...
} catch (IOException e) {
// handle exception
}
AutoCloseable
objects can be opened in the try
-block (within the ()
) and will be automatically closed instead of using the finally
block as in the code example above.
From the Oracle docs:
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. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.
So, this means that all objects that are AutoCloseable
can be used this way which means that e.g. ResultSet
and other resources can be used in the try-with-resources way. IMO, this simplifies the coding and readability.
However, readability is not the killer argument for why to use the new way. I believe that it is the simple fact that the resources are automatically closed. When used prior to Java 7 it was possible to forget to do null-checks or to close the underlying resource - try-with-resources is simply less error-prone.
But, with that said. It is not required to use try-with-resources, it is still possible to use it the old-school way even though I would not recommend it due (since it is both verbose and error-prone).

- 18,651
- 6
- 63
- 77
-
-
-
@jhamon OP didn't actually ask *why* to use it. He asked 'is this really required, ...' – user207421 Jan 08 '15 at 08:25
-
Agree with @EJP, what I think is, it not really required, it just merely increase the complexity by saving just two lines of code to close the connection or stream. – Atul_15 Jan 09 '15 at 09:15
-
@Atul_15 What does that mean? By using try-with-resources to automatically close e.g. an `InputStream` saves quite a few lines. E.g. the `close` operation can otherwise throw an exception, null checks must be present, finally-block must be present and so on. I think that try-with-resources is a **big** improvement for both readability and to actually avoid errors. – wassgren Jan 09 '15 at 09:18
-
@wassgren, but you really think that this is necessary , I don't think , becauase if java is doing it by itself then if you don't think there are many if-else clause in that method to close the connection or stream or to handle which type of errors. I think it is really not a good idea for this enhancement in JDK 1.7 according to performance point of view. – Atul_15 Jan 09 '15 at 11:00
-
@Atul_15 What I am saying is that you **should** let Java do it for you. Use try-with-resources, don't use try/catch/finally with manual closing. – wassgren Jan 09 '15 at 11:02
Read the documentation of the try-with-resources feature introduced in Java 7.
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. Any object that implementsjava.lang.AutoCloseable
, which includes all objects which implementjava.io.Closeable
, can be used as a resource.
And the Javadoc of AutoCloseable#close()
:
Closes this resource, relinquishing any underlying resources. This method is invoked automatically on objects managed by the
try
-with-resources statement.
This means you can create your own subtypes of AutoCloseable
resources and use them in this statement.

- 71,713
- 13
- 134
- 174
try (BufferedReader br = new BufferedReader(new InputStreamReader)) {
// try block
} catch (IOException e) {
// No need to close resourse. Only handle exception. Resource will be closed automatically
}
There are many classes that are available in java that implements Autoclosable.
Below link presents list of classes that implements this interface.
http://docs.oracle.com/javase/7/docs/api/java/lang/AutoCloseable.html

- 794
- 3
- 8
- 18