Articles

Java try with resources explained with examples

Java try with resources explained with examples

Java try with resources is an alternative of try-catch-finally block introduced in java 1.7

 

Java try with resources is actually a try statement that allows us to declare single or multiple resources within it. The benefit of declaring resources within try with resources is that closure of the resources is automatically taken care by this statement itself. 

 

Whereas, in normal try-catch-finally statement, it is programmers responsibility to close all the declared resources that were opened in it as shown below.

 

Normal try-catch-finally example

Given below is a simple try-catch-finally examples that uses a resource (BufferedReader) and then has to take care of closing it manually.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TIHDemo {

	public static void main(String[] args) {
		
		BufferedReader read = null;
		try{
			read = new BufferedReader(new FileReader("D:/Rajneesh.txt"));
			System.out.println(read.readLine());
		}catch(Exception e){
			System.out.println("Exception occured in code");
		}finally{
			try {
				read.close();//close the resource
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
}

In the above code:

  • It is programmars duty to close the resource (BufferedReader) that they are using. If you don't close it then a warning will appear - 'Resource leak: 'read' is never closed'
  • Generally the opened resources are closed in the finally block as coding convention. So we have closed it in finally in above code

.But the code looks messy and untidy. Isn't it?

Now suppose you are using multiple resources instead of just one. How incomprehensible the code might become. Just think.

This is where try with resources comes as saviour.


Use of try with resource in java code helps:

  • makes code better comprehensible
  • reduce lines of code
  • eliminate the headache of closing the resources like database connection or open files manually.

try with resources example

We will understand the try with resources with above example.

Lets try to convert the above example into try with resources.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class TIHDemo {

	public static void main(String[] args) throws FileNotFoundException, IOException {
		
		try(BufferedReader read = new BufferedReader(new FileReader("D:/Rajneesh.txt"))){			
			System.out.println(read.readLine());
		}
	}
}

This is the try with resources form of the normal try-catch-finally that we saw in first example.

 

Above code is:

  • neat and more comprehensible,
  • resource (BufferedReader) is declared within try statement.
  • Once the use of the resource is complete and try is about to terminate it automatically close all the declared resources within it. Thus we don't need to bother closing them within finally block.

 

So start declaring your resources within try statement and use try with resources as shown above without bothering much about closing the resources yourself.


Java Interview Questions

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Rohanjit Kumar
Technology geek, loves to write and share knowledge with the world. Having 9+ years of IT experience. B.Tech in Computer Science & Engineering
Page Views :    Published Date : Aug 25,2020  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!