Questions tagged [autocloseable]

java.lang.AutoCloseable is an interface which, when implemented by a class, can be used in the try-with-resources Statement. It is ensured that resource/class object is closed at the end of the statement.

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.

Resources:

127 questions
84
votes
1 answer

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 =…
flakes
  • 21,558
  • 8
  • 41
  • 88
82
votes
4 answers

Close multiple resources with AutoCloseable (try-with-resources)

I know that the resource you pass with a try, will be closed automatically if the resource has AutoCloseable implemented. So far so good. But what do I do when i have several resources that I want automatically closed. Example with sockets; try…
asmb
  • 1,015
  • 2
  • 8
  • 11
50
votes
1 answer

Try with multiple Resource in Java

I am new in Java8, and I want to know if, for the AutoCloseable resource, I have to add a try for each resource, or it will work with the code above try (Connection conn = getConnection();) { Statement stmt = conn.createStatement(); …
en Lopes
  • 1,863
  • 11
  • 48
  • 90
38
votes
3 answers

What does idempotent method mean and what are the side effects in case of calling close method of java.lang.AutoCloseable?

Java docs of close() method of java.lang.AutoCloseable says Note that unlike the close() method of Closeable, this close() method is not required to be idempotent. In other words, calling this close method more than once may have some visible…
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
31
votes
5 answers

Why is try-with-resources catch block selectively optional?

I read that the catch block in try-with-resources is optional. I've tried creating a Connection object in a try-with-resources block, with no subsequent catch block, only to get compiler error from eclipse: "Unhandled exception type SQLException…
Mer
  • 762
  • 1
  • 6
  • 12
29
votes
5 answers

In Java, how to check that AutoCloseable.close() has been called?

I am authoring a java library. Some of the classes that are meant to be used by library users, hold native system resources (over JNI). I'd like to ensure that the user "disposes" these objects, as they are heavy, and in a testsuite they may cause…
haelix
  • 4,245
  • 4
  • 34
  • 56
25
votes
1 answer

How is in Java the idempotence of the close() method of the Closeable interface ensured?

The Closeable interface was introduced in Java 5 whereas the AutoCloseable interface came in Java 7 together with the try-with-resources statement. Closeable extends (since Java 7) the Autocloseableinterface. In the book OCA/OCP Java SE 7 -…
Mathias Bader
  • 3,585
  • 7
  • 39
  • 61
19
votes
1 answer

Why is close() method of the resource called before catch in a try-with-resources construct in Java?

I happened to realize, that this is the case. See this example below: public class AutoClosableTest { public static void main(String[] args) throws Exception { try (MyClosable instance = new MyClosable()) { if (true) { …
Gábor Lipták
  • 9,646
  • 2
  • 59
  • 113
14
votes
3 answers

Using AutoClosable interfaces inside Stream API

Today I tried to refactor this code, that reads ids from files in a directory, Set ids = new HashSet<>(); for (String fileName : fileSystem.list("my-directory")) { InputStream stream = fileSystem.openInputStream(fileName); …
AdamSkywalker
  • 11,408
  • 3
  • 38
  • 76
13
votes
1 answer

AutoCloseable "resource leak" warning for factory created instances?

These "resource leak" warnings I'm getting in Eclipse for AutoCloseables seem to be a life-saver. However, how do I get them to work for factory created instances? For example (a works, but b doesn't): public static void main(String[] args) { //…
antak
  • 19,481
  • 9
  • 72
  • 80
12
votes
2 answers

Can my AutoCloseable.close() implementation detect a potential exception?

When implementing an AutoCloseable to work with the Java 7 try-with-resources statement, I would like to know if there had been an exception within the try block. E.g.: class C implements AutoCloseable { @Override public void close() { …
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
11
votes
2 answers

Right way to close CloseableHttpResponse/CloseableHttpClient

I'm using CloseableHttpResponse (from apache-httpclient-4.5.3) and I'm not sure I'm using it right, I saw an answer with no votes to use EntityUtils.consume on finally: CloseableHttpResponse response1 = httpclient.execute(httpGet); try { …
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
11
votes
4 answers

Array or collection of "Autocloseable" in Java8

Autocloseable should always be used with try-with-resources. At least Intellij inspection suggests it. So, if I have a code that produces Foo that implements Autocloseable I should do: try (final Foo foo = getFoo()) { foo.doSomething(); } But…
user996142
  • 2,753
  • 3
  • 29
  • 48
11
votes
2 answers

Why Java7 introduces AutoCloseable specially?

AutoCloseable is introduced in jdk1.7 and Cloesable is already in jdk1.5. And According to https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html The try-with-resources statement ensures that each resource is closed at…
JaskeyLam
  • 15,405
  • 21
  • 114
  • 149
10
votes
3 answers

What's a clean way to time code execution in Java?

It can be handy to time code execution so you know how long things take. However, I find the common way this is done sloppy since it's supposed to have the same indentation, which makes it harder to read what's actually being timed. long start =…
Mark
  • 5,089
  • 2
  • 20
  • 31
1
2 3
8 9