What are you closing? In general, closing a file open for writing (std::ostream
, FILE*
or a system dependent file descriptor) must be done before destruction, so that you can check for errors after having done the close, and report them. There are exceptions, however, and in particular, classes which wrap an open file should generally close it in their destructor (without checking for errors, since there's nothing you can do about them), in order to ensure proper clean up in case of an exception.
Presumably, an exception before the close means that there has been an error already, and the file being written will not be used. I generally wrap output in a class with a commit
function. commit
closes, and checks for errors. If the destructor is called before commit
, it closes, without checking for errors, and then removes the file being written, since it is probably not complete or usable.