<?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[Exception Handling, Vererbung und Polymorphie]]></title><description><![CDATA[<p>Hallo!</p>
<p>Ich will gerade ein Exception Handling für ein Projekt implementieren und würde dabei gern einen catch-Block schreiben, der alle Fehler die zu einer Gruppe gehören fängt und mir dann sagt, welcher Fehler der Gruppe es ist. Code-mäßig habe ich mir das wie folgt vorgestellt:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

class UnspecifiedError {
	public:
		virtual const char* getType() { return &quot;UnspecifiedError&quot;; };
};

class SpecifiedError1 : public UnspecifiedError {
	public:
		const char* getType() { return &quot;SpecifiedError1&quot;; };
};

class SpecifiedError2 : public UnspecifiedError {
	public:
		const char* getType() { return &quot;SpecifiedError2&quot;; };
};

int main() {
	UnspecifiedError e1;
	SpecifiedError1 e2;
	SpecifiedError2 e3;

	try { throw(e1); } catch(UnspecifiedError e) { std::cout &lt;&lt; e.getType() &lt;&lt; std::endl; }
	try { throw(e2); } catch(UnspecifiedError e) { std::cout &lt;&lt; e.getType() &lt;&lt; std::endl; }
	try { throw(e3); } catch(UnspecifiedError e) { std::cout &lt;&lt; e.getType() &lt;&lt; std::endl; }

	return 0;
}
</code></pre>
<p>Die Ausgabe ist dann aber erwartungsgemäß</p>
<pre><code>UnspecifiedError
UnspecifiedError
UnspecifiedError
</code></pre>
<p>Die gewünschte Ausgabe ist aber</p>
<pre><code>UnspecifiedError
SpecifiedError1
SpecifiedError2
</code></pre>
<p>Im catch-Block fange ich ja nunmal das konkrete Objekt und nicht einen Zeiger mit dem man dann Polymorphie betreiben könnte.</p>
<p>Hat jemand eine gute Idee, wie man etwas derartiges bewerkstelligen könnte? Gibt es dazu schon Lösungen?</p>
<p>Vielen Dank und viele Grüße,<br />
Maik</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/246799/exception-handling-vererbung-und-polymorphie</link><generator>RSS for Node</generator><lastBuildDate>Thu, 17 Sep 2026 20:50:46 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/246799.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 01 Aug 2009 20:39:03 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Exception Handling, Vererbung und Polymorphie on Sat, 01 Aug 2009 20:40:15 GMT]]></title><description><![CDATA[<p>Hallo!</p>
<p>Ich will gerade ein Exception Handling für ein Projekt implementieren und würde dabei gern einen catch-Block schreiben, der alle Fehler die zu einer Gruppe gehören fängt und mir dann sagt, welcher Fehler der Gruppe es ist. Code-mäßig habe ich mir das wie folgt vorgestellt:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

class UnspecifiedError {
	public:
		virtual const char* getType() { return &quot;UnspecifiedError&quot;; };
};

class SpecifiedError1 : public UnspecifiedError {
	public:
		const char* getType() { return &quot;SpecifiedError1&quot;; };
};

class SpecifiedError2 : public UnspecifiedError {
	public:
		const char* getType() { return &quot;SpecifiedError2&quot;; };
};

int main() {
	UnspecifiedError e1;
	SpecifiedError1 e2;
	SpecifiedError2 e3;

	try { throw(e1); } catch(UnspecifiedError e) { std::cout &lt;&lt; e.getType() &lt;&lt; std::endl; }
	try { throw(e2); } catch(UnspecifiedError e) { std::cout &lt;&lt; e.getType() &lt;&lt; std::endl; }
	try { throw(e3); } catch(UnspecifiedError e) { std::cout &lt;&lt; e.getType() &lt;&lt; std::endl; }

	return 0;
}
</code></pre>
<p>Die Ausgabe ist dann aber erwartungsgemäß</p>
<pre><code>UnspecifiedError
UnspecifiedError
UnspecifiedError
</code></pre>
<p>Die gewünschte Ausgabe ist aber</p>
<pre><code>UnspecifiedError
SpecifiedError1
SpecifiedError2
</code></pre>
<p>Im catch-Block fange ich ja nunmal das konkrete Objekt und nicht einen Zeiger mit dem man dann Polymorphie betreiben könnte.</p>
<p>Hat jemand eine gute Idee, wie man etwas derartiges bewerkstelligen könnte? Gibt es dazu schon Lösungen?</p>
<p>Vielen Dank und viele Grüße,<br />
Maik</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1753726</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1753726</guid><dc:creator><![CDATA[maikg]]></dc:creator><pubDate>Sat, 01 Aug 2009 20:40:15 GMT</pubDate></item><item><title><![CDATA[Reply to Exception Handling, Vererbung und Polymorphie on Sat, 01 Aug 2009 20:42:31 GMT]]></title><description><![CDATA[<p>Indem du einfach nicht ganze Objekte, sondern die Adresse der Objekte mit throw wirfst du den catch-Block anpasst. <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/1753733</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1753733</guid><dc:creator><![CDATA[Physik0r]]></dc:creator><pubDate>Sat, 01 Aug 2009 20:42:31 GMT</pubDate></item><item><title><![CDATA[Reply to Exception Handling, Vererbung und Polymorphie on Sat, 01 Aug 2009 20:46:20 GMT]]></title><description><![CDATA[<p>versuch es mal mit virtual</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1753735</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1753735</guid><dc:creator><![CDATA[sasdasdasd]]></dc:creator><pubDate>Sat, 01 Aug 2009 20:46:20 GMT</pubDate></item><item><title><![CDATA[Reply to Exception Handling, Vererbung und Polymorphie on Sat, 01 Aug 2009 20:53:53 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;iostream&gt;

class UnspecifiedError {
    public:
        virtual const char* getType() { return &quot;UnspecifiedError&quot;; };
};

class SpecifiedError1 : public UnspecifiedError {
    public:
        virtual const char* getType() { return &quot;SpecifiedError1&quot;; }; // virtual
};

class SpecifiedError2 : public UnspecifiedError {
    public:
        virtual const char* getType() { return &quot;SpecifiedError2&quot;; }; // virtual
};

int main() 
{
    try { throw UnspecifiedError(); } catch(UnspecifiedError&amp; e /* REFERENZ! &amp; */) { std::cout &lt;&lt; e.getType() &lt;&lt; std::endl; }
    try { throw SpecifiedError1(); } catch(UnspecifiedError&amp; e /* REFERENZ! &amp; */) { std::cout &lt;&lt; e.getType() &lt;&lt; std::endl; }
    try { throw SpecifiedError2(); } catch(UnspecifiedError&amp; e /* REFERENZ! &amp; */) { std::cout &lt;&lt; e.getType() &lt;&lt; std::endl; }

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1753742</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1753742</guid><dc:creator><![CDATA[sasdasdasd]]></dc:creator><pubDate>Sat, 01 Aug 2009 20:53:53 GMT</pubDate></item><item><title><![CDATA[Reply to Exception Handling, Vererbung und Polymorphie on Sat, 01 Aug 2009 21:06:20 GMT]]></title><description><![CDATA[<p>Dumme Frage am Rande: Polymorphie funktioniert doch nur bei Dynamischen Objekten?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1753745</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1753745</guid><dc:creator><![CDATA[DummDiDumm]]></dc:creator><pubDate>Sat, 01 Aug 2009 21:06:20 GMT</pubDate></item><item><title><![CDATA[Reply to Exception Handling, Vererbung und Polymorphie on Sat, 01 Aug 2009 21:16:37 GMT]]></title><description><![CDATA[<p>Vielen Dank für eure super-schnellen Antworten.</p>
<p>Ich habe die zwei getType()-Funktionen virtuell gemacht und jetzt folgende main-Funktion, die eure Vorschläge umsetzt (und die gewünschte Ausgabe liefert).</p>
<pre><code class="language-cpp">int main() {
	UnspecifiedError e1;
	try { throw(&amp;e1); } catch(UnspecifiedError* e) { std::cout &lt;&lt; e-&gt;getType() &lt;&lt; std::endl; }

	try { SpecifiedError1 e2; throw(&amp;e2); } catch(UnspecifiedError* e) { std::cout &lt;&lt; e-&gt;getType() &lt;&lt; std::endl; }

	try { throw(&amp;SpecifiedError2()); } catch(UnspecifiedError* e) { std::cout &lt;&lt; e-&gt;getType() &lt;&lt; std::endl; }

	try { throw(SpecifiedError2()); } catch(UnspecifiedError&amp; e) { std::cout &lt;&lt; e.getType() &lt;&lt; std::endl; }

	return 0;
}
</code></pre>
<p>Die erste Zeile ist die direkte Umsetzung von Physik0r's Vorschlag.</p>
<p>Für die dritte Zeile bekomme ich die Warnung, das ich eine Referenz auf ein temporäres Objekt werfe. Aber ist passiert das nicht auch in Zeile 2 und 4 auch (zumal der catch-Block später auch in einer anderen Funktion stehen soll als der try-Block)?</p>
<p>Polymorphie muss ich mir wohl nochmal anschauen, ich dachte das geht nur im Zusammenhang mit Zeigern...</p>
<p>Noch eine letzte Frage, warum ist das virtual in den abgeleiteten Klassen nötig? Reicht nicht für Polymorphiy das virtual in der Basisklasse?</p>
<p>Grüße, Maik</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1753754</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1753754</guid><dc:creator><![CDATA[maikg]]></dc:creator><pubDate>Sat, 01 Aug 2009 21:16:37 GMT</pubDate></item><item><title><![CDATA[Reply to Exception Handling, Vererbung und Polymorphie on Sat, 01 Aug 2009 21:29:15 GMT]]></title><description><![CDATA[<p>Ja, das virtual reicht in der Oberklasse, wie du es bei deinem Eingangsposting gemacht hast.</p>
<p>Meiner Meinung nach sollte man das virtual allerdings auch in den Unterklassen wiederholen, damit jemand, der nur die Unterklassen sieht, weiß, was Sache ist.</p>
<p>Fehler wirft man als Objekte (wie in deinem ersten Beispiel) und fängt sie als Referenzen. Also:</p>
<pre><code class="language-cpp">int main() {
    UnspecifiedError e1;
    SpecifiedError1 e2;
    SpecifiedError2 e3;

    try { throw e1; } 
    catch(UnspecifiedError &amp;e) { std::cout &lt;&lt; e.getType() &lt;&lt; std::endl; }
    try { throw e2; } 
    catch(UnspecifiedError &amp;e) { std::cout &lt;&lt; e.getType() &lt;&lt; std::endl; }
    try { throw e3; } 
    catch(UnspecifiedError &amp;e) { std::cout &lt;&lt; e.getType() &lt;&lt; std::endl; }

    return 0;
}
</code></pre>
<p>Oder auch gleich so:</p>
<pre><code class="language-cpp">int main() {
    try { throw UnspecifiedError(); } 
    catch(UnspecifiedError &amp;e) { std::cout &lt;&lt; e.getType() &lt;&lt; std::endl; }
    try { throw SpecifiedError1(); } 
    catch(UnspecifiedError &amp;e) { std::cout &lt;&lt; e.getType() &lt;&lt; std::endl; }
    try { throw SpecifiedError2; } 
    catch(UnspecifiedError &amp;e) { std::cout &lt;&lt; e.getType() &lt;&lt; std::endl; }

    return 0;
}
</code></pre>
<p>Stefan.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1753763</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1753763</guid><dc:creator><![CDATA[DStefan]]></dc:creator><pubDate>Sat, 01 Aug 2009 21:29:15 GMT</pubDate></item></channel></rss>