Skip to content

This is to illustrate a way to accidentally leave a resource unclosed in Java, even if you use a try-with-resources block

License

Notifications You must be signed in to change notification settings

cinnamon-rolls/unclosed-resources-risk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

unclosed-resources-risk

This is to illustrate a way to accidentally leave a resource unclosed in Java, even if you use a try-with-resources block

Consider the following Java code:

try (EvilResource resource = new EvilResource()) {
  resource.doThings();
} catch (IOException e) {
  System.out.println("Catch: " + e.getMessage());
}
public EvilResource() throws IOException {
  System.out.println("I am constructing...");
  
  // open another resource, such as an InputStream
  // store a reference to that data to close it later in #close()
  // e.g. this.childStream1 = new BufferedInputStream(...);
  
  // an exception of some kind is thrown
  throw new IOException("Uh oh! (constructor)");
}

If the EvilResource constructor throws an Exception, then the program will not crash, but the EvilResource#close() method is not called. Clearly, the author intended to close the resource. This results in a subtle risk for the program to leave uncaught resources.

All code is under the Unlicense, so use it for whatever you'd like.

About

This is to illustrate a way to accidentally leave a resource unclosed in Java, even if you use a try-with-resources block

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published