<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Catch exceptions by reference]]></title><description><![CDATA[<p>Ich lese gerade More Effective C++ und da steht, man soll exceptions by reference fangen, hier ist ein Beispielcode:</p>
<pre><code class="language-cpp">void someFunction()                  // nothing changes in this
{                                    // function
  ...

  if (a validation test fails) {
    throw Validation_error();
  }

  ...

}

void doSomething()
{
  try {
    someFunction();                  // no change here
  }
  catch (exception&amp; ex) {            // here we catch by reference
                                     // instead of by value

    cerr &lt;&lt; ex.what();               // now calls
                                     // Validation_error::what(),
    ...                              // not exception::what()
  }
}
</code></pre>
<p>Was mir bei leibe nicht klar ist, wieso soll das funktionieren? Wird die exception mittels throw im scope gelassen?<br />
Mein Compiler meckert da auch, dass er sagt, das 'ex' eine unreferenzierte lokale variable ist</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/226158/catch-exceptions-by-reference</link><generator>RSS for Node</generator><lastBuildDate>Thu, 01 Oct 2026 02:19:43 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/226158.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 30 Oct 2008 07:53:48 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Catch exceptions by reference on Thu, 30 Oct 2008 07:53:48 GMT]]></title><description><![CDATA[<p>Ich lese gerade More Effective C++ und da steht, man soll exceptions by reference fangen, hier ist ein Beispielcode:</p>
<pre><code class="language-cpp">void someFunction()                  // nothing changes in this
{                                    // function
  ...

  if (a validation test fails) {
    throw Validation_error();
  }

  ...

}

void doSomething()
{
  try {
    someFunction();                  // no change here
  }
  catch (exception&amp; ex) {            // here we catch by reference
                                     // instead of by value

    cerr &lt;&lt; ex.what();               // now calls
                                     // Validation_error::what(),
    ...                              // not exception::what()
  }
}
</code></pre>
<p>Was mir bei leibe nicht klar ist, wieso soll das funktionieren? Wird die exception mittels throw im scope gelassen?<br />
Mein Compiler meckert da auch, dass er sagt, das 'ex' eine unreferenzierte lokale variable ist</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1606962</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1606962</guid><dc:creator><![CDATA[Seikilos]]></dc:creator><pubDate>Thu, 30 Oct 2008 07:53:48 GMT</pubDate></item><item><title><![CDATA[Reply to Catch exceptions by reference on Thu, 30 Oct 2008 08:07:58 GMT]]></title><description><![CDATA[<p>Klar, die Exception lebt bis der catch-Block abgearbeitet ist. Sonst würde das Fangen by-value ja auch nicht funktionieren, da beim Erstellen der Kopie das Original bereits weg wäre.</p>
<p>Die unreferenzierte lokale Variable bekommst Du, wenn Du ex nicht verwendest. Das hat mit Exceptions nichts zu tun.</p>
<p>Ausserdem, wenn Du immer by-value fangen würdest, würdest Du nie eine std::exception fangen können, wenn du einen std::runtime_error geworfen hast. Höchstens einen Teilausschnitt davon.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1606974</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1606974</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Thu, 30 Oct 2008 08:07:58 GMT</pubDate></item><item><title><![CDATA[Reply to Catch exceptions by reference on Thu, 30 Oct 2008 08:09:14 GMT]]></title><description><![CDATA[<p>Exceptions werden &quot;in eine sichere Umgebung kopiert&quot; (steht das nicht im selben Kapitel?) und auf die bekommst Du dann eine Referenz. Die ursprüngliche wird tatsächlich abgebaut (oder &quot;darf abgebaut werden&quot; ? weiß gerade nicht genau)...</p>
<p>Seikilos schrieb:</p>
<blockquote>
<p>...<br />
...Mein Compiler meckert da auch, dass er sagt, das 'ex' eine unreferenzierte lokale variable ist</p>
</blockquote>
<p>Hmmmm - mit dem obigen Code glaube ich das aber nicht....</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1606976</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1606976</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Thu, 30 Oct 2008 08:09:14 GMT</pubDate></item><item><title><![CDATA[Reply to Catch exceptions by reference on Thu, 30 Oct 2008 08:17:01 GMT]]></title><description><![CDATA[<p>Ok, nun verstehe ich.</p>
<p>Danke</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1606981</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1606981</guid><dc:creator><![CDATA[Seikilos]]></dc:creator><pubDate>Thu, 30 Oct 2008 08:17:01 GMT</pubDate></item></channel></rss>