<?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[Sauberes Exception-Handling]]></title><description><![CDATA[<p>Hi</p>
<p>Ich finde aus irgendeinem Grund keine saubere Lösung für try-throw-catch. Es soll so sein, dass im catch-Block die Exception nicht zerstört werden muss (z.B. mit delete).</p>
<p>Wie ich das sehe, gibt es zwei Möglichkeiten zum Werfen von Exceptions:</p>
<pre><code class="language-cpp">class CException;
...
1. throw CException;
2. throw new CException;
</code></pre>
<p>Und zum Fangen der Exceptions:</p>
<pre><code class="language-cpp">1. catch ( CException* pExc )
2. catch ( CException Exc )
3. catch ( CException&amp; Exc )
</code></pre>
<p>Jetzt funktioniert aber keine dieser Kombinationen befriedigend. Bei <em>throw CException</em> wird einmal der Konstruktor und 3 mal (mit catch (CException Exc) bzw 2 mal (mit catch (CException&amp; Exc) der Destruktor aufgerufen.<br />
Das &quot;Beste&quot; scheint mir noch <em>throw new CException</em> mit <em>catch ( CException</em> pExc )*. Nur dabei wird die Exception nie gelöscht, was unter Umständen zu großen Speicherleaks führen kann.</p>
<p>Hat vielleicht jemand eine gute Lösung, ohne Leaks und ohne die Exception im catch-Block explizit zu zerstören?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/165196/sauberes-exception-handling</link><generator>RSS for Node</generator><lastBuildDate>Mon, 14 Sep 2026 11:27:30 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/165196.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 16 Nov 2006 13:20:14 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Sauberes Exception-Handling on Thu, 16 Nov 2006 13:20:14 GMT]]></title><description><![CDATA[<p>Hi</p>
<p>Ich finde aus irgendeinem Grund keine saubere Lösung für try-throw-catch. Es soll so sein, dass im catch-Block die Exception nicht zerstört werden muss (z.B. mit delete).</p>
<p>Wie ich das sehe, gibt es zwei Möglichkeiten zum Werfen von Exceptions:</p>
<pre><code class="language-cpp">class CException;
...
1. throw CException;
2. throw new CException;
</code></pre>
<p>Und zum Fangen der Exceptions:</p>
<pre><code class="language-cpp">1. catch ( CException* pExc )
2. catch ( CException Exc )
3. catch ( CException&amp; Exc )
</code></pre>
<p>Jetzt funktioniert aber keine dieser Kombinationen befriedigend. Bei <em>throw CException</em> wird einmal der Konstruktor und 3 mal (mit catch (CException Exc) bzw 2 mal (mit catch (CException&amp; Exc) der Destruktor aufgerufen.<br />
Das &quot;Beste&quot; scheint mir noch <em>throw new CException</em> mit <em>catch ( CException</em> pExc )*. Nur dabei wird die Exception nie gelöscht, was unter Umständen zu großen Speicherleaks führen kann.</p>
<p>Hat vielleicht jemand eine gute Lösung, ohne Leaks und ohne die Exception im catch-Block explizit zu zerstören?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1175948</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1175948</guid><dc:creator><![CDATA[Badestrand]]></dc:creator><pubDate>Thu, 16 Nov 2006 13:20:14 GMT</pubDate></item><item><title><![CDATA[Reply to Sauberes Exception-Handling on Thu, 16 Nov 2006 13:24:31 GMT]]></title><description><![CDATA[<p>throw CException; und fangen per const ref.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1175951</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1175951</guid><dc:creator><![CDATA[..........]]></dc:creator><pubDate>Thu, 16 Nov 2006 13:24:31 GMT</pubDate></item><item><title><![CDATA[Reply to Sauberes Exception-Handling on Thu, 16 Nov 2006 13:26:08 GMT]]></title><description><![CDATA[<p><a href="http://www.parashift.com/c++-faq-lite/exceptions.html" rel="nofollow">http://www.parashift.com/c++-faq-lite/exceptions.html</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1175952</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1175952</guid><dc:creator><![CDATA[cleaner]]></dc:creator><pubDate>Thu, 16 Nov 2006 13:26:08 GMT</pubDate></item><item><title><![CDATA[Reply to Sauberes Exception-Handling on Thu, 16 Nov 2006 16:09:42 GMT]]></title><description><![CDATA[<p>OK, danke erstmal <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1176066</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1176066</guid><dc:creator><![CDATA[Badestrand]]></dc:creator><pubDate>Thu, 16 Nov 2006 16:09:42 GMT</pubDate></item><item><title><![CDATA[Reply to Sauberes Exception-Handling on Thu, 16 Nov 2006 23:58:02 GMT]]></title><description><![CDATA[<p>Wie macht std::runtime_error das? Irgendwie schafft es das, dass der Destruktor nur <strong>einmal</strong> aufgerufen wird! Es klappt zwar damit, aber irgendwie fände ich es schöner, wenn es auch ohne std::runtime_error gehen würde <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1176221</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1176221</guid><dc:creator><![CDATA[Badestrand]]></dc:creator><pubDate>Thu, 16 Nov 2006 23:58:02 GMT</pubDate></item><item><title><![CDATA[Reply to Sauberes Exception-Handling on Fri, 17 Nov 2006 00:30:40 GMT]]></title><description><![CDATA[<blockquote>
<p>throw CException; und fangen per const ref</p>
</blockquote>
<p>Wieso const?<br />
Mir ist der Sinn von const bekannt, allerdings sehe ich<br />
hier den Sinn nicht. Nur damit man für den catch-Block<br />
sieht, dass die Exception nicht verändert wird?<br />
Oder was soll das?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1176225</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1176225</guid><dc:creator><![CDATA[ConstException]]></dc:creator><pubDate>Fri, 17 Nov 2006 00:30:40 GMT</pubDate></item><item><title><![CDATA[Reply to Sauberes Exception-Handling on Fri, 17 Nov 2006 00:40:51 GMT]]></title><description><![CDATA[<p>Manche Compiler können dadurch besser optimieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1176228</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1176228</guid><dc:creator><![CDATA[Neku]]></dc:creator><pubDate>Fri, 17 Nov 2006 00:40:51 GMT</pubDate></item><item><title><![CDATA[Reply to Sauberes Exception-Handling on Fri, 17 Nov 2006 00:55:04 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/13210">@Badestrand</a>:<br />
std::runtime_error macht garnix besonderes, Exceptions werden halt nicht sinnlos in der Gegend rumkopiert. Wenn du sie per value fängst wird halt der Copy-Konstruktor aufgerufen, auch klar, du fängst ja eine Kopie. Mit Slicing und allem was daran noch hässlich ist. Es wird aber sicher nie 3x der Destruktor aufgerufen obwohl nur 1x ein Objekt konstruiert wurde, das kannst du mir glauben. Zumindest nicht wenn der Compiler richtig arbeitet, und DAS haben die Compiler eigentlich so ziemlich alle raus -- sogar VC6 baut da keinen Mist.</p>
<p>Und warum man oft das &quot;catch by const-ref&quot; statt &quot;catch by non-const-ref&quot; sieht liegt wohl daran dass es (anscheinend) compiler gibt oder gegeben hat die die Exception kopieren wenn man sie &quot;by non-const-ref&quot; fängt, oder sonstwie langsameren Code erzeugen.</p>
<p>Und... poste mal nen Stück Code wo mehr als 1x der Destruktor aufgerufen wird, und ich sage dir dann was faul ist <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1176229</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1176229</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Fri, 17 Nov 2006 00:55:04 GMT</pubDate></item></channel></rss>