Article of interest: Python pros/cons for the enterprise

Matthew Woodcraft mattheww at chiark.greenend.org.uk
Sat Feb 23 18:48:26 EST 2008


Jeff Schwab  <jeff at schwabcenter.com> wrote:
>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.

I see. Then, unless you don't care about data loss passing silently,
this 'most traditional' way to open a file is unsuitable for files
opened for writing.

-M-



More information about the Python-list mailing list