• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Finally {IOException}

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I created an I/O stream say FileInputStream, when i am over using it i call its close() method. But my IDE shows error and suggest it to be wrapped inside a try-catch block.
Now if exception occurs in closing the stream how will it finally get closed? Even i put the close() statement in 'finally block', it suggests to enclose in try-catch block.
Thats really confusing.
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could write something like this:
 
Ranch Hand
Posts: 312
MS IE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may do as follows,


 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Madhan: your example is mess!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not really. It's the only way to separate IOExceptions thrown by the close() method from IOExceptions thrown by "actual" code. Still, I prefer the shorter version that does not make the distinction. It looks like Andrey's code but doesn't need the null check:
But the Java 7 way, with try-with-resources, is even better:
The first line is new syntax, and it will make sure fis is automatically closed when the try block ends.
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hate the ugly code that results from having try-catch in a finally clause so I have an IO utility class that contains a forceClose() method -



I just call this in the finally clause.

Note - one doesn't actually need the 'null' check since a NullPoinerException will be thrown and caught but ...
 
Ashish Malik
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess i am not clear about this...
Firstly, what are the reason for i/o stream to throw exception?
Secondly, why wouldn't it throw if it is inside try or finally or anywhere? If we can have a try inside another try's finally clause...then it should be try inside try inside try and so on!
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ashish Malik wrote:Firstly, what are the reason for i/o stream to throw exception?


While doing I/O all kinds of things could happen. For instance (and this actually happened to me once) your Ethernet cable could be disconnected. Then communication isn't possible any more. How else would the jvm communicate this problem?

Ashish Malik wrote:Secondly, why wouldn't it throw if it is inside try or finally or anywhere? If we can have a try inside another try's finally clause...then it should be try inside try inside try and so on!

If you have multiple resources then yes you'll need to nest a lot of try-catch-finally statements. That's why they invented try-with-resources in Java 7.
 
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic