In my previous article, I have explained how to use try-catch-final in java,  in this article I am going to cover  try-with -resources which is an automatic resource management in Java Exception handling
The try-with-resources is introduced in Java 1.7. the main advantage of this you do not need to explicitly close resources in the finally block, the try-with-resource will help us to automatically close’s resources once all statements within the try block executed successfully 
You can use try-with-resources only for classes that implement java.lang.AutoCloseable interface

Example: try-catch block

public void loadExcelFile() throws IOException {
		FileInputStream excelFile = null;
		XSSFWorkbook excelWBook = null;
		XSSFSheet excelWSheet = null;
		try {
			File myFile = new File("data/data.xls");
			excelFile = new FileInputStream(myFile);
			excelWBook = new XSSFWorkbook(excelFile);
			excelWSheet = excelWBook.getSheet("Screen1");
			excelWBook.close();
			excelFile.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (excelWBook != null) {
				excelWBook.close();
				excelFile.close();
			}
		}
	}

Example: try-with-resource

In the below example, the object for FileInputStream is created inside the parenthesis. once the code execution is completed it will automatically close, because the FileInputStream implements an AutoCloseable interface

try (FileInputStream excelFile = new FileInputStream(myFile))
package com.keys.examples;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelManager {
	public ExcelManager() {
	}
	private static Logger Log = Logger.getLogger(ExcelManager.class);
	private static XSSFSheet ExcelWSheet;
	private static XSSFWorkbook ExcelWBook;
	private static XSSFCell Cell;
	public static void main(String[] args) throws IOException, Exception {
		DOMConfigurator.configure("log4j.xml");
		File myFile = new File("inputdata/data.xlsx");
		try (FileInputStream excelFile = new FileInputStream(myFile)) {
			ExcelWBook = new XSSFWorkbook(excelFile);
			ExcelWSheet = ExcelWBook.getSheet("Screen1");
			if (ExcelWSheet.getRow(100) == null) {
				Log.error("Error occured while reading excel file");
			}
		}
                catch (Exception e) {
			Log.error("Error occured while reading excel file" + e.getMessage());
		}
	}
}

I am intentionally throwing exception here if you go and see the logs you will not see the exception which is in the catch block instead of that you will see the exception which is in the try block because, in the try-with-resource, if any error occurs it will not throw to catch block instead of that it will throw into try block itself

By coderss

Leave a Reply

Your email address will not be published. Required fields are marked *