<?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 in c++]]></title><description><![CDATA[<p>Hi<br />
Ich wollte mir einmal exception handling in c++ anschauen.<br />
dazu hab ich mir eine exception klasse ( myexception ) von exception abgeleitet.</p>
<p>wenn ich die allerdings werfe, dann bricht mein programm ab :(.<br />
also hab ich noch irgendwas nicht ganz richtig verstanden:</p>
<pre><code class="language-cpp">#include&lt;iostream&gt;
#include &quot;MyException.h&quot;

using namespace std;

class DBException : public myException 
{
public:
    DBException(const string&amp; reason) : myException(reason) {}
};

void eingabe () throw (DBException*)
{
string s;
cin &gt;&gt; s;
throw DBException(&quot;bla&quot;);
}

int main (int argc, char **argv)
{
	try
	{
		eingabe();
	}
	catch (DBException* e)
	{
		cerr &lt;&lt; (e-&gt;what()) &lt;&lt; endl;
	}

	try
	{
		eingabe();
	}
	catch (DBException* e)
	{
		cerr &lt;&lt; (e-&gt;what()) &lt;&lt; endl;
		exit(1);
	}
return 0;
}
</code></pre>
<p>wie man sieht rufe ich eingabe 2 mal auf.<br />
bei mir bricht das programm allerdings beim ersten cache block mit core dumped ab.</p>
<p>was mach ich falsch ?</p>
<p>danke<br />
Verucca</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/178485/exception-handling-in-c</link><generator>RSS for Node</generator><lastBuildDate>Sun, 20 Sep 2026 21:15:38 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/178485.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 11 Apr 2007 15:56:21 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to exception handling in c++ on Wed, 11 Apr 2007 15:56:21 GMT]]></title><description><![CDATA[<p>Hi<br />
Ich wollte mir einmal exception handling in c++ anschauen.<br />
dazu hab ich mir eine exception klasse ( myexception ) von exception abgeleitet.</p>
<p>wenn ich die allerdings werfe, dann bricht mein programm ab :(.<br />
also hab ich noch irgendwas nicht ganz richtig verstanden:</p>
<pre><code class="language-cpp">#include&lt;iostream&gt;
#include &quot;MyException.h&quot;

using namespace std;

class DBException : public myException 
{
public:
    DBException(const string&amp; reason) : myException(reason) {}
};

void eingabe () throw (DBException*)
{
string s;
cin &gt;&gt; s;
throw DBException(&quot;bla&quot;);
}

int main (int argc, char **argv)
{
	try
	{
		eingabe();
	}
	catch (DBException* e)
	{
		cerr &lt;&lt; (e-&gt;what()) &lt;&lt; endl;
	}

	try
	{
		eingabe();
	}
	catch (DBException* e)
	{
		cerr &lt;&lt; (e-&gt;what()) &lt;&lt; endl;
		exit(1);
	}
return 0;
}
</code></pre>
<p>wie man sieht rufe ich eingabe 2 mal auf.<br />
bei mir bricht das programm allerdings beim ersten cache block mit core dumped ab.</p>
<p>was mach ich falsch ?</p>
<p>danke<br />
Verucca</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1263944</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1263944</guid><dc:creator><![CDATA[Verucca]]></dc:creator><pubDate>Wed, 11 Apr 2007 15:56:21 GMT</pubDate></item><item><title><![CDATA[Reply to exception handling in c++ on Wed, 11 Apr 2007 16:11:39 GMT]]></title><description><![CDATA[<p>Naja, Du versuchst, einen Pointer zu fangen, wo Du ein Objekt wirfst ....<br />
Kann auch sein, dass Dein Programm bei der throw()-Spezifikation rausfliegt (hängt aber von Compiler und -einstellungen ab).</p>
<p>Fang lieber Referenzen (das mit dem &quot;Objekte werfen&quot; ist schon gut so), das macht weniger Probleme.</p>
<pre><code class="language-cpp">#include&lt;iostream&gt;
#include &quot;MyException.h&quot;

using namespace std;

class DBException : public myException
{
public:
    DBException(const string&amp; reason) : myException(reason) {}
};

void eingabe () throw (DBException)
{
   string s;
   cin &gt;&gt; s;
   throw DBException(&quot;bla&quot;);
}

int main (int argc, char **argv)
{
    try
    {
        eingabe();
    }
    catch (DBException&amp; e)
    {
        cerr &lt;&lt; e.what() &lt;&lt; endl;
    }

    try
    {
        eingabe();
    }
    catch (DBException&amp; e)
    {
        cerr &lt;&lt; e.what() &lt;&lt; endl;
        exit(1);
    }
   return 0;
}
</code></pre>
<p>Außerdem ist es keine schlechte Idee, seine exceptions von std::runtime_error und seinen Kindern abzuleiten (gibts per #incude &lt;stdexcept&gt; ).</p>
<p>Ach ja: Die throw-Spezifikation wird nur zur Laufzeit überprüft (falls überhaupt) und führt dann zu einem abort() (sieht auch ein wenig wie ein &quot;core dump&quot; aus) ... ist also anders, als man das zu Anfang denkt. Ich persönlich nutze sie deswegen nicht mehr, weil sie mir zu wenig Gewinn und zuviel Aufhebens sind. (außer da, wo es natürlich sein muss wg. fremder API&amp;Co)</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1263956</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1263956</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Wed, 11 Apr 2007 16:11:39 GMT</pubDate></item><item><title><![CDATA[Reply to exception handling in c++ on Wed, 11 Apr 2007 16:08:19 GMT]]></title><description><![CDATA[<p>Das Programm müsste eigentlich ganz normal via <code>terminate</code> beendet werden.</p>
<p>Ändere deine catch-clauses in <code>DBException&amp;</code> , und lass die Exception-Spezifikation am besten ganz weg.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1263963</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1263963</guid><dc:creator><![CDATA[finix]]></dc:creator><pubDate>Wed, 11 Apr 2007 16:08:19 GMT</pubDate></item><item><title><![CDATA[Reply to exception handling in c++ on Wed, 11 Apr 2007 16:16:36 GMT]]></title><description><![CDATA[<p>lo<br />
naja die genaue meldung ist:</p>
<blockquote>
<p>terminate called after throwing an instance of 'DBException'<br />
what(): bla<br />
Aborted (core dumped)</p>
</blockquote>
<p>und ableiten tu ich das wie gesagt von exception:</p>
<pre><code class="language-cpp">#ifndef MYEXCEPTION_H_
#define MYEXCEPTION_H_
#include&lt;exception&gt;
#include&lt;string&gt;

using namespace std;

class myException : public exception {
public:
    myException(const string&amp; reason) : reason(reason) {}
    virtual ~myException() throw() {}
    virtual const char* what() const throw() 
    {
        return reason.c_str();
    }

private:
    const string reason;
}; 

#endif /*MYEXCEPTION_H_*/
</code></pre>
<p>jedenfalls macht es bei der meldung keinen unterschied ob ich referenz auf das objekt oder den pointer typ übergebe .... habs gerade ausprobiert.<br />
die Meldung bleibt die gleiche..</p>
<p>lg<br />
Verucca</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1263971</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1263971</guid><dc:creator><![CDATA[Verucca]]></dc:creator><pubDate>Wed, 11 Apr 2007 16:16:36 GMT</pubDate></item><item><title><![CDATA[Reply to exception handling in c++ on Wed, 11 Apr 2007 16:36:52 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>lass doch mal Deinen aktuellen Code sehen ...</p>
<p>Folgender Code läuft bei mir (gcc 3.4.4) nämlich wie gewünscht:</p>
<pre><code class="language-cpp">using namespace std;

class myException : public exception {
public:
    myException(const string&amp; reason) : reason(reason) {}
    virtual ~myException() throw() {}
    virtual const char* what() const throw()
    {
        return reason.c_str();
    }

private:
    const string reason;
}; 

class DBException : public myException
{
public:
    DBException(const string&amp; reason) : myException(reason) {}
};

void eingabe () throw (DBException)
{
   string s;
   cin &gt;&gt; s;
   throw DBException(&quot;bla&quot;);
}

int main (int argc, char **argv)
{
    try
    {
        eingabe();
    }
    catch (DBException&amp; e)
    {
        cerr &lt;&lt; e.what() &lt;&lt; endl;
    }

    try
    {
        eingabe();
    }
    catch (DBException&amp; e)
    {
        cerr &lt;&lt; e.what() &lt;&lt; endl;
        exit(1);
    }
   return 0;
}
</code></pre>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1263985</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1263985</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Wed, 11 Apr 2007 16:36:52 GMT</pubDate></item><item><title><![CDATA[Reply to exception handling in c++ on Wed, 11 Apr 2007 16:41:48 GMT]]></title><description><![CDATA[<p>Verucca schrieb:</p>
<blockquote>
<p>jedenfalls macht es bei der meldung keinen unterschied ob ich referenz auf das objekt oder den pointer typ übergebe .... habs gerade ausprobiert.<br />
die Meldung bleibt die gleiche..</p>
</blockquote>
<p>Das <code>terminate</code> kommt durch deine throw-specification - ändere die wie Simon2 es vorschlägt, oder lass sie besser noch ganz weg.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1264003</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1264003</guid><dc:creator><![CDATA[finix]]></dc:creator><pubDate>Wed, 11 Apr 2007 16:41:48 GMT</pubDate></item><item><title><![CDATA[Reply to exception handling in c++ on Wed, 11 Apr 2007 16:43:33 GMT]]></title><description><![CDATA[<p>finix schrieb:</p>
<blockquote>
<p>...oder lass sie besser noch ganz weg.</p>
</blockquote>
<p>Wie Simon2 es ebenfalls vorschlägt... <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>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1264006</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1264006</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Wed, 11 Apr 2007 16:43:33 GMT</pubDate></item><item><title><![CDATA[Reply to exception handling in c++ on Wed, 11 Apr 2007 18:24:59 GMT]]></title><description><![CDATA[<p>hi<br />
ja betriebsblindheit würde ich sagen <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>
<p>ich hab alle pointer entfernt nur beim throw nicht ......</p>
<pre><code class="language-cpp">void eingabe () throw (DBException*)
</code></pre>
<p>hier war immer noch der pointer drin .....</p>
<p>funktioniert alles - naja wenigstens hab ich das alles so halbwegs richtig verstanden....</p>
<p>danke war genau was ich gebraucht hab ....</p>
<p>Aber um mein verständnis zu fördern.<br />
In allen beispielen die ich so im google gefunden hab hat niemand eine exception klasse abgeleitet.<br />
Im prinzip haben die immer nur eine einfache klasse mit der methode what() deffiniert aber nie vererbt.<br />
Warum stell ich mir hier die frage?</p>
<p>lg<br />
Verucca</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1264099</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1264099</guid><dc:creator><![CDATA[Verucca]]></dc:creator><pubDate>Wed, 11 Apr 2007 18:24:59 GMT</pubDate></item><item><title><![CDATA[Reply to exception handling in c++ on Wed, 11 Apr 2007 18:31:57 GMT]]></title><description><![CDATA[<p>Hm, Unwissenheit? <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="😉"
    /><br />
Ich finde es generell gut, auch eigene Exceptions <strong>immer</strong> von std::exception (oder dessen Kinder runtime_error und logic_error) abzuleiten, da man dann die Standard-Exceptions zusammen mit den eigenen abfangen kann (indem man die gemeinsame Basis fängt), wenn es ungeeignet erscheint eine der konkreten Exceptions zu behandeln.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1264105</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1264105</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Wed, 11 Apr 2007 18:31:57 GMT</pubDate></item><item><title><![CDATA[Reply to exception handling in c++ on Wed, 11 Apr 2007 18:36:39 GMT]]></title><description><![CDATA[<p>Verstehe na dann ists ja gut - wenn ich einmal was geschreiben hab mach ich sowieso nur noch copy und paste - deshalb will ich das immer vernüftig haben ...</p>
<p>Wie man sieht tu ich mir in c++ aber immer noch mit den pointern ein bisschen schwer .... auch wenn ichs grundsätzlich verstehe finde ich es einfach immer noch ziemlich unübersichtlich - aber ich denke mal man gewöhnt sich daran ...<br />
lg<br />
Verucca</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1264112</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1264112</guid><dc:creator><![CDATA[Verucca]]></dc:creator><pubDate>Wed, 11 Apr 2007 18:36:39 GMT</pubDate></item></channel></rss>