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
4
votes
1 answer

How to close aws client in aws lambda

I am trying to correctly write an aws lambda using Java that will use aws sdk SqsClient and SnsClient. I see that these clients implement close() method, and it is generally a good practice to call this method when client is no longer required. And…
4
votes
1 answer

Exception-safely return Autoclosable object

When you want to use some AutoClosable object you should use try-with-resources. Ok. But what if I want to write a method, that returns an AutoClosable? After you've created or received an AutoCloseable object, you should close it in case of…
a.khakh
  • 115
  • 10
4
votes
2 answers

Is it safe to close object before removing it from LinkedHashMap?

I want to override LinkedHashMap's removeEldestEntry(Map.Entry) method to remove stale mappings automatically. Also I want to cleanup the entry that would be removed. The entry is an AutoCloseable object. Can I call close() on it if it's going to…
eriee
  • 416
  • 2
  • 15
4
votes
3 answers

Implementing AutoCloseable - How do I know if an exception occurred in the try block?

We have a class we've written which opens up a connection to a server. When you're done with it, you need to either tell it to commit if everything was successful, or tell it to rollback if something went wrong. So right now we have a lot of spots…
ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
4
votes
3 answers

Why can a use block not safely initialize a var?

Why does this give a compile error? val autoClosable = MyAutoClosable() var myVar: MyType autoClosable.use { myVar= it.foo() } println(myVar) // Error: Variable 'myVar' must be initialized Maybe the compiler just sees { myVar= it.foo() } as a…
Marco Eckstein
  • 4,448
  • 4
  • 37
  • 48
4
votes
3 answers

Helper to call close() if implements AutoCloseable?

Is there any helper method in the JDK or common libraries that does this: if (resource instanceof AutoCloseable) { ((AutoCloseable) resource).close(); } Just a one-liner to call an object's close() if applicable. I know about…
tariksbl
  • 1,069
  • 6
  • 20
4
votes
2 answers

Why is BufferedReader not closed when obtaining `Stream` in try-with-resources?

The reader should be closed when a Stream is used in a try-with-resources. Given this: try(Stream lines = new BufferedReader(reader).lines()) { return lines.map(it -> trim ? it.trim() : it) …
The Coordinator
  • 13,007
  • 11
  • 44
  • 73
3
votes
1 answer

Is "TypedArray should be recycled" a false positive in Lint when using try-with-resources?

I have read the dozens of questions here on SO regarding recycling TypedArrays, but I guess they are a bit too old and written before we could widely use try-with-resource statements, so none of them talk about using the AutoCloseable implementation…
avalancha
  • 1,457
  • 1
  • 22
  • 41
3
votes
1 answer

Mark a method as returning an existing closeable to avoid spurious warning at usage site

Given a class MyClass using an internal closeable object, myCloseable and providing a method getCloseable() that returns it; Eclipse, if configured for this kind of closeable resource warnings, will systematically warn each time anyone calls…
Olivier Cailloux
  • 977
  • 9
  • 24
3
votes
2 answers

Should closing a resource always be a responsibility of the function that opens it?

public ResultSet executeQuery(String query) { return session.call(query) } public List findRecords(String name) { String query = prepareQuery(name); return mapResultToRecord(executeQuery(query)); } public List
6harat
  • 542
  • 6
  • 18
3
votes
2 answers

use try-with-resources to close a Closeable

I have a Map and if a key is removed from the map I want to close the Closeable. Normally I have something like: Closeable c = map.remove(key); c.close(); My Eclipse warns me "Resource 'c' should be managed by try-with-resource", so…
Madjosz
  • 509
  • 1
  • 5
  • 13
3
votes
2 answers

Java proxy for Autocloseable (Jedis resources)

I am trying to find out whether it is possible to create Java dynamic proxy to automatically close Autocloseable resources without having to remember of embedding such resources with try-resources block. For example I have a JedisPool that has a…
Konrad
  • 1,605
  • 3
  • 24
  • 45
3
votes
3 answers

Catch errors during object construction from try-with-resources separately from body

Summary I have a closeable type, CloseableClass that can throw an IOError in its constructor, methods and maybe even inside close. I want to use try-with-resources and still deal with errors during construction differently to errors during use (use…
Veedrac
  • 58,273
  • 15
  • 112
  • 169
3
votes
3 answers

Is it meaningful for AutoCloseable's close method to throw an exception? How should this be handled?

In C#, it is considered bad practice to throw exceptions in the Dispose method of an IDisposable. By contrast, in java the close method of AutoCloseable allows any Exception whatsoever to be thrown and forces the caller to handle it somehow. But…
George Simms
  • 3,930
  • 4
  • 21
  • 35
2
votes
0 answers

Is there a tidy way to do chained initialisation logic in Kotlin?

After opening a file, someone has to close it. Kotlin has the convenient use { ... } method for the case where you're doing all the work with the file in-place. But sometimes, you have to do some other things with the file and return another object…
Hakanai
  • 12,010
  • 10
  • 62
  • 132
1 2
3
8 9