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

springboot return 200 when close Closeable even throw exception

How to return error in springboot and close a Closeable class? When I got some error in springboot and close a closable class is returning 200 OK don't I need close the Closeable? Is there some way to handle this on springboot? CSVPrinter…
Dilermando Lima
  • 1,004
  • 8
  • 21
0
votes
1 answer

JavaMail-Folder AutoClosable exception

Since JavaMail version 1.6.0 classes Store and Folder (amongst Transport) should implement AutoClosable interface. I did not find any examples of someone using JavaMail API with auto-closable. After a few tests I am not sure how to exactly use this…
Filou
  • 490
  • 4
  • 17
0
votes
1 answer

How FileInputStream instance is automatically created when Closeable interface is implemented by some custom class when using try-with-resources

I have been working with try-with-resources statement. try(FileReader rd = new FileReader("Test.txt");){} catch (Exception e) {e.printStackTrace();} The benefit of using try with resources is mainly to do with avoiding to specify the finally block…
0
votes
1 answer

Does ActiveMQManagedConnection (EAP/AMQ) support Autoclosable?

I am using: openshift AMQ (seems to be a forked activemq-5.11.0.redhat...... version) EAP 7.2.3 While local debugging I get as Connection some ~ConnectionProxy with a physical ActiveMQManagedConnection Can I use "try-with-resources"? I am not…
user1782357
  • 313
  • 2
  • 11
0
votes
1 answer

Ensure Autocloseable is closed in Cucumber step definitions

My Cucumber Java step definitions can create AutoCloseable objects, which can acquire external (perhaps scarce) resources. Those objects should have their close() method called when they are no longer needed, to free those resources. How do I ensure…
Raedwald
  • 46,613
  • 43
  • 151
  • 237
0
votes
1 answer

Can i modify ReentrantLock so one can use it with try with resources?

I wanted to ask if this implementation is ok, or maybe there are some issues that can be later problematic. The idea is to implement a ReentrantLock class that can be used with 'try with resources' and that means that it has to implement the…
0
votes
0 answers

Why is Cleaner action not invoked?

This is a follow-up question from this one. Specifically I'm trying to verify correct use of an API which involves calling close() on Java objects holding heavy native resources over JNI. To re-iterate from that question, this is needed in both…
haelix
  • 4,245
  • 4
  • 34
  • 56
0
votes
3 answers

AutoCloseable and throws exception

The AutoCloseable has a method 'void close throws Exception'. Some suggest to extend this interface and override the close method and remove the Exception. See for instance the below link. But when i forget to put the statement in a…
0
votes
2 answers

Why does AutoClosaeble use try/catch semantics?

Java's AutoCloseable feature "reuses" try/catch semantics: try (Stream x = ...) { // work with x } // x is out of scope and was auto-closed I'm curious why they didn't introduce new semantics for this new feature. try implies that you expect…
Josh M.
  • 26,437
  • 24
  • 119
  • 200
0
votes
1 answer

Neo4j Driver in play

I use a Neo4j java driver in my play app. Currently I initialize a new driver for every controller (ie. for every http call). When the autoclosable close method runs it seems to block the entire call for about almost two seconds. Running without…
oht
  • 357
  • 4
  • 15
0
votes
2 answers

How to Force AutoClosable Warning to Propagate to Caller of Class in Java?

I'm new to Java's AutoClosable interface (but not new to Java). I'm trying to force client code which requests an instance of WebDriver to close the driver after it finishes using it (by calling driver.quit()) I want a way to force any method that…
0
votes
1 answer

Is this a proper Implementation of database connection and autocloseable resources?

I have a program that reads from a MySQL database table, iterates through the resultSet and returns the row data for a particular userId in a List. With the help of all the available internet resources, I managed to create something that works but…
Roshan Upreti
  • 1,782
  • 3
  • 21
  • 34
0
votes
1 answer

Selective catch for a closeable resource

This is more of a syntax / structuring issue. I'm working with JDBC: Statement and ResultSet, which means SQLExceptions are being thrown everywhere. My code looks like this: private static final String MY_QUERY = "SELECT * FROM MY_TABLE"; public…
Jefferson
  • 379
  • 2
  • 4
  • 11
0
votes
1 answer

How to implement Autocloseable for resource opened on construction

I am have a resource I open on construction of my object. I use it to write throughout the lifetime of the object. But my application can close without warning and I need to capture this. The class is pretty straightforward. public class…
jiveturkey
  • 2,484
  • 1
  • 23
  • 41
0
votes
1 answer

Business logic inside AutoCloseable close method

A good practice is to has business logic inside the close method (AutoCloseable interface) and not invoke the logic manually but via try-with-resource ? I have a scenario where I write stream way to XML file with use of jax-b and I need to manually…
G. Sz.
  • 55
  • 1
  • 2
  • 4
1 2 3
8 9