• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Try Catch While Reading File Line By Line

 
Ranch Hand
Posts: 51
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am reading an input file line by line in Java.
When i read the line; i have some business processing to perform.
This can throw an exception which needs to be caught.
Hence, i surround my code with try catch.

In a scenario where exception occurs; how do i close the opened streams :
1. I added finally section. However, the code there must be written in try catch !!! What if an exception occurs while closing the stream. It could lead to memory leakage or any other issue.

Please refer to code snippet below:



Request you to please provide inputs.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When i read the line; i have some business processing to perform.
This can throw an exception which needs to be caught.
Hence, i surround my code with try catch.


If you have a block of code that opens a stream, does some processing and then closes the stream then you should always close the stream within a finally clause (or use Java 7's try-with-resources statement) and not just because your business logic may thrown an exception.

If your finally clause has to close 2 or more streams then you need to make sure that any exceptions thrown by closing a stream are handled so that the subsequent streams are also closed. An alternative approach is to close each subsequent stream in the finally clause of the proceeding streams try-catch block ie:
You can either catch and handle the exceptions here or at a higher level. I've not shown the catch statements here to make it easier to see the nested try-finally statment
 
Tarun Trehan
Ranch Hand
Posts: 51
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your inputs.
I was aware of the fact that streams must be closed.

However, the nested try-catch-finally do not look that neat.
Thanks for the inputs Tony.
 
Rancher
Posts: 3571
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that in Java 7, it's much nicer to use the try-with-resources construct. The following does essentially the same thing, auto-closing the streams when you leave the try block in which they are declared:

I would probably drop the catch IOException and let that be thrown instead, since whatever code called loadDateFormate() will probably be upset if the formats aren't loaded. Also, if you're using Java 7, we might as well take advantage of other convenient classes and methods:
 
Tarun Trehan
Ranch Hand
Posts: 51
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,

Thanks for your inputs.

For now, i have gone the traditional way to ensure streams are closed.
Though i am not on Java 7; but you've listed out some really nice features.
 
There is no "i" in denial. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic