Article of interest: Python pros/cons for the enterprise
Jeff Schwab
jeff at schwabcenter.com
Sat Feb 23 17:47:25 EST 2008
Matthew Woodcraft wrote:
> Jeff Schwab <jeff at schwabcenter.com> wrote:
>> The most traditional, easiest way to open a file in C++ is to use an
>> fstream object, so the file is guaranteed to be closed when the fstream
>> goes out of scope.
>
> Out of interest, what is the usual way to manage errors that the
> operating system reports when it closes the file?
By default, the fstream object just sets its "failbit," which you can
check manually by calling my_stream.fail(). If you want anything
particular to take place on failure to close a stream, you either have
to call close manually, or you need a dedicated object whose destructor
will deal with it.
Alternatively, you can tell the fstream ahead of time that you want
exceptions thrown if particular actions fail. There's a convention that
destructors don't ever throw exceptions, though, so it would be unusual
to request an exception when close() fails.
More information about the Python-list
mailing list