Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a FAQ item: <a href="http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.3" rel="nofollow noreferrer"><strong>17.3</strong> How can I handle a destructor that fails?</a> </p> <p>Edit:</p> <blockquote> <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.</p> </blockquote> <p>No. Dtors for <code>y</code> and <code>z</code> <strong>will be called</strong> when the stack unwinds provided you have a <code>try-catch</code> block installed around the <code>MyFile ... z.close()</code> part. A better idea would be to put a <code>close</code> in the dtor as well. The dtor for <code>x</code> will not be -- so some cleanup is in order in the <code>catch</code> block.</p> <p>I suggest you run the following program to better understand dtor-calls in the case of an exception (once as-is and again by uncommenting the <code>S x</code> line):</p> <pre><code>#include &lt;iostream&gt; using namespace std; struct s { s(int i = 0) : m_i( i ) { cout &lt;&lt; __func__ &lt;&lt; endl; } ~s() { if (m_i == 0) except(); cout &lt;&lt; __func__ &lt;&lt; endl; } void except() { throw 42; } int m_i; }; int main() { try { s y(2), z(3); /* s x */ y.except(); } catch (...) { cout &lt;&lt; "exception\n"; } cout &lt;&lt; "stack unwound\n"; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload