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
3 answers

Why do we have to do both declare and define resources in try-with-resource block?

try(PrintWriter f = new PrintWriter(new BufferedWriter(new FileWriter("abc.txt")));) {} catch(IOException ex) { ex.printStackTrace(); } Above works fine. But when I do PrintWriter f; try(f = new PrintWriter(new BufferedWriter(new…
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
-1
votes
2 answers

Does close() method of the Closeable interface has exception in the signature

I was reading Closeable and AutoCloseable interface for the first time. As per my understanding we can't throw any exception apart from the IOException from close method of the Closeable interface and we can throw any possible exception like…
Sia Saxena
  • 21
  • 5
-1
votes
1 answer

How to use try-with-resources for java.util.logging.FileHandler?

I'm having below code and creating log file using java.util.logging.FileHandler. In this scenario, I should manually close the resources in finally block. try { fh = new FileHandler("Test.log"); logger.addHandler(fh); ... } catch (IOException e)…
Bharathiraja
  • 714
  • 1
  • 12
  • 20
-1
votes
2 answers

Java- How can you close `FileInputStream` in finally block if the same method returns that `FileInputStream`

Sonar is showing a bug for the below code which is of blocker type. It's complaining that I have not closed FileInputStream but how can I do that? FileInputStream is the return type of method and if I close here then it will be of no use from where…
Ashish Pancholi
  • 4,569
  • 13
  • 50
  • 88
-1
votes
1 answer

What's best practice to close resoures?

I have a java class which has allocated resources that must be closed. I don't really know how to enforce this, my current approach is implementing the interface AutoCloseable, hoping that the caller calls close() sometimes and adding a boolean…
user3561614
  • 1,024
  • 1
  • 12
  • 20
-2
votes
1 answer

Java Autocloseable Test

public class CloseableResource implements AutoCloseable { private static boolean _closed = false; int _n; public CloseableResource(int n){ } public void use() throws Exception{ throw new Exception("Exception"); } …
Gabriel N
  • 13
  • 2
-2
votes
2 answers

Java AutoClosable behaviour in function

I have a sample code here. Will the FileInputStream created by the function, get automatically closed when the code exists the try/catch block of parentFunction ? Or does it need to be explicitly closed in the someOtherFunction() itself ? private…
CodeKata
  • 572
  • 4
  • 8
1 2 3
8
9