How to handle a file destructor throwing an exception?
<p>It's important to detect an error when you close a file to which you were writing, because the last part of your data could be flushed during the close, and if it's lost then your last write effectively failed and someone should know about it.  The destructor for a file object is a nice place to automatically close it, but people say not to throw exceptions from destructors, so if the close fails then how do you know about it?</p>

<p>I've heard people recommend calling the close() method of the file manually.  That's sounds nice, except what happens if the close() methods of multiple files all fail in a situation like this:</p>

<pre><code>    MyFile x(0), y(1), z(2);
    x.close();
    y.close();
    z.close();
</code></pre>

<p>?</p>

<p>Well, it seems that if the close() method of 'x' throws an exception then you've done well to uphold the rule to avoid throwing exceptions in the destructor of 'x', except now you're good-intentioned early calls to the close() methods of 'y' and 'z' won't execute until their destructors.  So, then when the close() method of 'y' is called in the destructor of 'y' or the close() method of 'z' is called in the destructor of 'z', and if they do throw exceptions then you're screwed.</p>

<p>Is there a reasonable way to not be screwed in such a situation?</p>
