try-with-resources Questions
5
Solved
Failing to call shutdown() on a thread executor will result in a never terminating application.
Best practice to shut down the ExecutorService is this:
ExecutorService service = null;
try {
ser...
Lens asked 30/12, 2016 at 9:0
5
With reference to my question Any risk in a AutoCloseable wrapper for java.util.concurrent.locks.Lock?, I am wondering why the try-with-resource-statement require a named local variable at all.
My ...
Gavette asked 16/5, 2013 at 13:30
3
In Java 17 I have a serializer that generates a tree structure. Before generating child entities, I increase the indention level; afterwards I decrease the indention level. Normally that should be ...
Plumley asked 26/9, 2022 at 20:11
4
Solved
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 ...
Disillusionize asked 7/12, 2016 at 11:56
2
Solved
I am using android MediaMetaDataRetriever which implements AutoCloseable in an android application. I have the below code
try (final MediaMetadataRetriever retriever = new MediaMetadataRetriever())...
Komsa asked 28/8, 2020 at 10:22
5
Solved
Coming from a C++ background, I am a huge fan of the RAII pattern. I have used it extensively to handle memory management and lock management along with other use cases.
With Java 1.7 I see that i...
Jehoshaphat asked 18/4, 2015 at 2:54
2
I'm having a warning message constantly, despite my code seems to be good. The message is:
WARNING: A connection to http://someurl.com was leaked. Did you forget to close a response body?
java.lan...
Augmenter asked 29/7, 2019 at 19:21
4
Solved
Here is the Main.java:
package foo.sandbox.db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Main {
public...
Sickert asked 14/11, 2015 at 0:43
3
Solved
I have a base class Base and a child class Child which extends it. Base implements java.lang.AutoCloseable.
Let's suppose that the constructor for Child throws a Foo.
Now consider
try (Base c = ...
Thereat asked 8/1, 2016 at 13:44
7
Solved
This is my very first question on SO and I'm confused there isn't a similar question yet!
So the question is:
Why doesn't try-with-resources work with field variables?
Or in other words: Why ...
Tutti asked 21/6, 2013 at 12:31
4
Solved
Is it possible to ignore the exception thrown when a resource is closed using a try-with-resources statement?
Example:
class MyResource implements AutoCloseable{
@Override
public void close() t...
Ravi asked 31/7, 2011 at 13:16
6
I have recently having some discussions with my professor about how to handle the basic jdbc connection scheme. Suppose we want to execute two queries, this is what he proposes
public void doQueri...
Oddson asked 26/3, 2014 at 19:55
2
The reader should be closed when a Stream is used in a try-with-resources.
Given this:
try(Stream<String> lines = new BufferedReader(reader).lines()) {
return lines.map(it -> trim ? it....
Reinsure asked 2/12, 2013 at 1:18
1
Solved
Background: I use the Java class InitialDirContext to access LDAP directories. Unfortunately, it does not implement interface AutoCloseable, so it cannot be used in try-with-resources blocks.
Here...
Pennywise asked 12/10, 2019 at 15:35
4
Solved
I'm wondering if putting a return statement inside a try-with-resources block prevents the resource to be automatically closed.
try(Connection conn = ...) {
return conn.createStatement().execute(...
Salchunas asked 8/4, 2014 at 20:42
5
Solved
When I tried to write an equivalent of a Java try-with-resources statement in Kotlin, it didn't work for me.
I tried different variations of the following:
try (writer = OutputStreamWriter(r.getOut...
Parks asked 17/11, 2014 at 9:48
5
Solved
Consider the following situation :
try (ResultSet resultSet = DriverManager.getConnection("jdbc:...", "user", "pass")
.createStatement().executeQuery(sql)) {
.
.
...
Retroflexion asked 29/11, 2020 at 12:11
5
Solved
I have this method where I am using try with resources of Java SE 7.
private void generateSecretWord(String filename){
try (FileReader files = new FileReader(filename)){
Scanner input = new Sca...
Neidaneidhardt asked 22/11, 2013 at 20:54
3
I saw this example on the web and in the Book "Effective Java" (by Joshua Bloch).
try(BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))){
writer.write(str); // do some...
Degeneration asked 18/8, 2020 at 8:5
2
Solved
According to doc of http://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#close() ,
When a Statement object is closed, its current ResultSet object, if
one exists, is also closed.
...
Deliverance asked 13/2, 2015 at 6:55
4
Solved
I had some trouble expressing the Java's try-with-resources construct in Kotlin. In my understanding, every expression that is an instance of AutoClosable should provide the use extension function....
Cool asked 1/2, 2016 at 17:31
5
Solved
I have been looking at code and I have seen try with resources. I have used the standard try-catch statement before and it looks like they do the same thing. So my question is Try With Resour...
Hurty asked 22/10, 2014 at 19:58
5
Solved
I have a method for getting users from a database with JDBC:
public List<User> getUser(int userId) {
String sql = "SELECT id, name FROM users WHERE id = ?";
List<User> users = new Ar...
Amorette asked 9/11, 2011 at 14:31
6
Solved
Will try-with-resources call flush() implicitly?
If it does, in the following code snippet, bw.flush() can be safely removed?
static void printToFile1(String text, File file) {
try (BufferedWrit...
Dukedom asked 1/9, 2015 at 5:57
2
Solved
In Java 7's try-with-resources, I don't know which order the finally block and the auto-closing happens. What's the order?
BaseResource b = new BaseResource(); // not auto-closeable; must be stop'...
Quinone asked 9/6, 2014 at 21:8
1 Next >
© 2022 - 2024 — McMap. All rights reserved.