-2

What is the purpose of try with resource block in java. I understands that it takes an AutoCloseable object reference like Inputstream, which it case close .

I want to know what the advantage of this construct. The same we are able to achieve by closing resources in finally block.

try(? extends AutoCloseable){
}
gprathour
  • 14,813
  • 5
  • 66
  • 90
Abhishek Saxena
  • 292
  • 3
  • 15
  • 1
    Java guarantees that the resources inside the `try` clause will be closed automatically regardless of what happens (save a `System.exit()`). – Tim Biegeleisen Jun 09 '17 at 10:41
  • 2
    Have you read the [tutorial](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)? If not, please read it. If you have, please highlight which bit is unclear. – biziclop Jun 09 '17 at 10:41

1 Answers1

0

You can achieve the same thing by closing resources in a finally block. Using try-with-resource is just slightly less verbose - it reduces the need for boilerplate code for resource management all over the place.

Riaan Nel
  • 2,425
  • 11
  • 18