• 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

try-catch-finally ( when and how to close files ??)

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi folks -
been studying from the book "Sun Certified Programmer for Java 2 Study Guide (Exam 310-025)", Second Edition, Copyright 2001 by The McGraw-Hill Companies.
here's some code with accompanying review question found on page 210:

the answer is that the program does not compile due to error on line 13 which is the line where we find "out.close()" in the finally section.
The book says "any method that throws a checked exception must be called within a try clause or the method must declare that it throws the exception.
SO, HOW WOULD YOU HANDLE CODING OF THE "out.close()" call?
a) place the code within a new try-catch block within the finally {}?
b) other ideas?
Note: earlier in the review part for this section, the authors specifically stated "This is the right place to close files, release your network sockets, and perform any other cleanup your code requires". Funny how the test review question seems to disprove that statement!
THANKS
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by david eberhardt:

SO, HOW WOULD YOU HANDLE CODING OF THE "out.close()" call?
a) place the code within a new try-catch block within the finally {}?


Correct! like this:
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Jessica.
I saw that your response is the same way it is suggested in Pete van der Linden's book "Just Java 2" (Fifth Edition) which is a pretty good book.
I also looked at a program an old work mate wrote which puts the "out.close()" call within the original try block of code. I'm guessing that if you put bothe the open and close within the top try block, you might not be able to tell which line might have casued the IOException, if one occurrs ???
[B] New Question - it looks to me that if I use the method that you mention, I might be able to isolate or identify whether it was a file opening or file closing exception more easily?
[ July 31, 2002: Message edited by: david eberhardt ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic