Skip to content
Advertisement

Tag: try-with-resources

Is try-with-resource not safe when declaring multiple effectively final resources?

Since Java 9 we can use effectively final variables in try-with-resources. The example below presents a situation where one of the resources initialization throws an exception. When I run this example, the only output I get is a RuntimeException, meaning that Resource1 was not closed. That was expected, since it wasn’t initialized in the try-with-resources. But, is this the expected

Why write Try-With-Resources without Catch or Finally?

Why write Try without a Catch or Finally as in the following example? Answer As explained above this is a feature in Java 7 and beyond. try with resources allows to skip writing the finally and closes all the resources being used in try-block itself. As stated in Docs Any object that implements java.lang.AutoCloseable, which includes all objects which implement

Advertisement