Week 108 — What is a `try`-with-resources statement and what is it useful for?
Question of the Week #108
What is a
try
-with-resources statement and what is it useful for?7 Replies
Many resources like files, database connections or network sockets should be closed as soon as they are no longer needed.
To do this, one could naively implement that similar to the following:
However, if an exception occurs, the
close()
method isn't called so the resource would stay open.
To resolve this problem, one could close the BufferedWriter
in a finally
block:
With the
finally
block ensuring that close()
is called, this seems like a good solution.
However, this can become inconvenient when operating with multiple resources:
This is annoying to write and small mistakes can result in the resources not being closed correctly. Furthermore, if an exception both within the try
block occurs in any of the close()
methods (which could happen for all sorts of reasons, for example a network connection going failing when writing/closing it), that exception would replace the original exception.📖 Sample answer from dan1st
try
-with-resources statement in Java makes sure that resources like files connections are automatically closed after use, which simplifies resource management and reduces the risk of leaks.Submission from thehsb
All I know for now as a beginner is that try-with-resources is used for I/O functions that will be automaticaly closed after we end our work with them.
Instead of writring try-catch with finally where in final part we will close the connection , it will be done automaticaly by the language
Submission from sett9824
A try with a resource is basicly a way to replace the "finaly" statement because it will automaticly close your in or out streams.
example:
Normaly:
You can also add multiple of them with a ';' to split them.
Submission from ufo.dev
try-with-resources is a mechanism to simplify resource creation and cleanup. For instance, working with an input stream in a "traditional" try block would look like this:
Contrast this with a similar try-with-resources statement, which removes a lot of the boilerplate code:
In this example, the
finally
block is not needed, since fis
will be closed automatically once the try
block completes (whether it finishes successfully or an exception is thrown).Some other items of note about try-with-resources:
- Any class that implements
java.lang.AutoCloseable
can be used inside the try()
block
- Multiple resources can be instantiated inside try()
, separated with a semicolon, e.g. try (InputStream is = ...; OutputStream os = ...)
- It is valid to have a try-with-resources statement without any catch or finally blocks, as long as the method propagates any potential exceptions via the throws
clause, and no additional cleanup is needed beyond the managed resources
- The resources will have been closed before any exception handler or finally block is run
- If the try
block generates an exception, then any exceptions generated when auto-closing the resource will be suppressed, and the exception from the try
block will be thrown. The suppressed exceptions can be retrieved with the Throwable::getSuppressed
method.⭐ Submission from dangerously_casual