Closing Files that Weren't Successfully Opened



Almost from as far back in my software education as I can remember,
people who would know have told me that it's good practice to close
one's files when one is done with them, so when I write programs that
open files I always try to do that. But I think I've found some grey
area that makes this policy a little ambiguous.

Whenever I write a program that uses the classes <File> and <Scanner>
to read in lines of text from a file, when I go to compile it my
compiler declares, "unreported exception
java.io.FileNotFoundException; must be caught or declared to be
thrown". And whenever I write a program that uses the classes
<FileWriter>, <BufferedWriter>, and <PrintWriter> to write lines of
text to a file, when I go to compile it my compiler gives a similar
declaration: "unreported exception java.io.IOException; must be caught
or declared to be thrown".

So, being pretty obedient to what the compiler tells me to do when I
can figure out what that is, I always add a <try-catch> block to
handle the mentioned exceptions.

My question is this. If I declare a variable <scnnr> to be of type
<Scanner>, and try to open it with the statement <scnnr = new
Scanner( new File( "Xy.Txt"))>, and a <FileNotFoundException> gets
thrown, should I still do a <scnnr.close()>, say in the <catch>
block? And similarly, if I declare <prntWrtr> to be of type
<PrintWriter> and try to open it with the statement <prntWrtr = new
PrintWriter( new BufferedWriter( new FileWriter( "Xy.Txt")))>, and an
<IOException> gets thown, should I still do a <prntWrtr.close()>, also
probably in the <catch> block? Or in each such situation can I
conclude that since an exception occurred while I was attempting to
open the respective file, the variables will still each both be
<null>, and therefore I don't have to do anything?

Kevin Simonson
.



Relevant Pages

  • Re: Properties
    ... Is there any reason why this would not work and not simplify the ... Why should I have to declare any variable most of the time? ... The reason you explicitly declare fields used by a property is that the ... compiler needs to know what the code in the property does. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: advice on package design
    ... > for following scope. ... the compiler would have to do ... the extra verbiage required just to declare X. ... Unfortunately this is also untrue in Ada. ...
    (comp.lang.ada)
  • Re: use of erf intrinsic
    ... while it is also legal to declare a referenced function ... within the calling functionor even block, a la classic FORTRAN, ... The implementation (compiler) is required to check that references ... If you use K&R1 syntax, the compiler is not required to detect ...
    (comp.lang.fortran)
  • Re: Primitive curiosity...
    ... those types after the compiler has already used the information. ... "dispatch table" or something like that. ... contains the addresses of the primitive subprograms of the type. ... When you declare a type extension, ...
    (comp.lang.ada)
  • Re: using iso_c_binding
    ... start with the ones the compiler noticed for you. ... I prefer to declare the type of a function in a separate type ... There are some funny special-case rules to ... Another one that g95 bitched about after I fixed all of the above was ...
    (comp.lang.fortran)