In my previous article, I have explained about exception handling, in this article, I am going to explain to you how to use try-catch block in Java
Let me take a simple example, the code which you see below will throw FileNotFoundException if there is no .xls file exists in the specified folder and it will break normal flow if you directly run the below code
The final block is used to close the streams or to close database connections etc…, in the below real-time example, we are using FileInputStream class to read the excel file which is an IO operation, so we must close it
The final block always executes whether error occurred or not, so we must use java final block to close our FileInputStream object
You can close stream object in the try block but what happens if some error occurs, the remaining code will not execute and finally end up will with memory leakage issue
A technology enthusiast and a professional blogger from India. Through out my IT career, I have had the pleasure of working on various new technologies and built products like www.ziprides.com and some other products. Unfortunately, my attempts have not given me the desired results and as a result, I have finally decided to build a professional blog where I would like to share all my learnings and hoping to learn from other enthusiasts around the world.