Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In both cases, since you catch by reference, you are effectively altering the state of the original exception object (which you can think of as residing in <a href="https://web.archive.org/web/20110824215142/http://cutebugs.net/files/cpp/ar01s02.html" rel="noreferrer">a magical memory location which will stay valid during the subsequent unwinding</a> -- <code>0x98e7058</code> in the example below). However,</p> <ol> <li>In the first case, since you rethrow with <code>throw;</code> (which, unlike <code>throw err;</code>, preserves the original exception object, with your modifications, in said "magical location" at <code>0x98e7058</code>) <strong><em>will</em> reflect the call to append()</strong></li> <li>In the second case, since you throw something explicitly, a <strong>copy</strong> of <code>err</code> will be created then thrown anew (at a different "magical location" <code>0x98e70b0</code> -- because for all the compiler knows <code>err</code> could be an object on the stack about to be unwinded, like <code>e</code> was at <code>0xbfbce430</code>, not in the "magical location" at <code>0x98e7058</code>), so <strong>you will lose derived-class-specific data</strong> during the copy-construction of a base class instance.</li> </ol> <p>Simple program to illustrate what's happening:</p> <pre><code>#include &lt;stdio.h&gt; struct MyErr { MyErr() { printf(" Base default constructor, this=%p\n", this); } MyErr(const MyErr&amp; other) { printf(" Base copy-constructor, this=%p from that=%p\n", this, &amp;other); } virtual ~MyErr() { printf(" Base destructor, this=%p\n", this); } }; struct MyErrDerived : public MyErr { MyErrDerived() { printf(" Derived default constructor, this=%p\n", this); } MyErrDerived(const MyErrDerived&amp; other) { printf(" Derived copy-constructor, this=%p from that=%p\n", this, &amp;other); } virtual ~MyErrDerived() { printf(" Derived destructor, this=%p\n", this); } }; int main() { try { try { MyErrDerived e; throw e; } catch (MyErr&amp; err) { printf("A Inner catch, &amp;err=%p\n", &amp;err); throw; } } catch (MyErr&amp; err) { printf("A Outer catch, &amp;err=%p\n", &amp;err); } printf("---\n"); try { try { MyErrDerived e; throw e; } catch (MyErr&amp; err) { printf("B Inner catch, &amp;err=%p\n", &amp;err); throw err; } } catch (MyErr&amp; err) { printf("B Outer catch, &amp;err=%p\n", &amp;err); } return 0; } </code></pre> <p>Result:</p> <pre><code> Base default constructor, this=0xbfbce430 Derived default constructor, this=0xbfbce430 Base default constructor, this=0x98e7058 Derived copy-constructor, this=0x98e7058 from that=0xbfbce430 Derived destructor, this=0xbfbce430 Base destructor, this=0xbfbce430 A Inner catch, &amp;err=0x98e7058 A Outer catch, &amp;err=0x98e7058 Derived destructor, this=0x98e7058 Base destructor, this=0x98e7058 --- Base default constructor, this=0xbfbce430 Derived default constructor, this=0xbfbce430 Base default constructor, this=0x98e7058 Derived copy-constructor, this=0x98e7058 from that=0xbfbce430 Derived destructor, this=0xbfbce430 Base destructor, this=0xbfbce430 B Inner catch, &amp;err=0x98e7058 Base copy-constructor, this=0x98e70b0 from that=0x98e7058 Derived destructor, this=0x98e7058 Base destructor, this=0x98e7058 B Outer catch, &amp;err=0x98e70b0 Base destructor, this=0x98e70b0 </code></pre> <p>Also see:</p> <ul> <li><a href="https://stackoverflow.com/questions/1654150/scope-of-exception-object-in-c">Scope of exception object in C++</a> </li> <li><a href="https://stackoverflow.com/questions/2274661/throwing-non-const-temporaries-by-reference">Throwing ... "by reference"</a></li> </ul>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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