Effective Java: try v/s try with resources

Reading Time: 3 minutes

It’s not uncommon to acquire resources while developing and forgetting to close them. For example, InputStream is a resource that would be explicitly needed to be closed and hence can be often overlooked.  To effectively work with our resources it is important that we return the resources as soon as we are done using them else they may lead to performance consequences. One of the ways we have ensured that these resources are closed is by placing them in the finally block! Let’s take a look.

“try-catch-finally”, the savior of resources

image1

Even in the case of an exception, a “finally” block can ensure that a resource has been closed. Though verbose, this has been a tried and tested go-to method for Java developers for a while now. But there are cases when this go-to method can give unprecedented results. For example, have a look here!

image2

If you won’t say it, I will. It looks ugly and ultra verbose. Added to that, it is easily possible to get confused about closing the resources. But it works. However, not exactly how one would want. What happens if the code in the “finally” block throws an exception? Let’s assume that in readFileLine( ) function, we can not acquire the resource because of the unavailability of the underlying hardware. Now this will throw an exception which will be handled. But close( ) will also give an exception. Now this exception will suppress the one that occurred before it. You won’t find the record of the exception which is the reason for the second one. This is a major issue as the most important tool in debugging our code is the stack trace. Here, since it’s a simple piece of code, you may be able to find somehow, what the source of errors was. But imagine something like this in gigantic production code! Scary isn’t it?

Can’t the resources close themselves?

Wouldn’t it be great if the resource knew when to close itself? Well, it’s not just you who thought that. Java 7 brought with itself the Autocloseable interface. A resource that implements the aforementioned interface overrides a void returning method called close( ). Many of the libraries implement it and in case you’re developing a resource, you should too. And what’s the point? The point is, this method gets invoked automatically when the flow of code exits the try-with-resource block.

Try with… what?

try-with-resource is a construct introduced in Java 7. It allows declaring the resources which implement Autocloseable interface initially, and at the end, they are autoclosed thanks to the interface mentioned above. Take a look at the previous code when altered with this construct.

image3

In this case, if the code throws an exception, and then the close also throws an exception, instead of suppressing the former exception it suppresses the latter ones. However,  each one of them is printed in the stack trace though it is mentioned if an exception was suppressed.

So in conclusion, not only is it neater to write using this construct but it is also easier to debug. For more blogs pertaining to coding effectively in java, stay tuned.

blog_footer

Written by 

Ayush Prashar is a software consultant having more than 1 year of experience. He is familiar with programming languages such as Java, Scala, C, C++ and he is currently working on reactive technologies like Lagom, Akka, Spark, and Kafka. His hobbies include playing table tennis, basketball, watching TV series and football.