<?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[std::bad_alloc Exception per Hand auslösen?]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich bin noch Anfänger und habe das erste Mal was von Exceptions gelesen und habe dazu eine Frage: Kann ich eine std::bad_alloc Exception per Hand auslösen, um das Verhalten, das bei einem Fehler bei new auftritt, zu simulieren?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/309026/std-bad_alloc-exception-per-hand-auslösen</link><generator>RSS for Node</generator><lastBuildDate>Wed, 05 Aug 2026 12:09:27 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/309026.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 12 Oct 2012 07:49:34 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to std::bad_alloc Exception per Hand auslösen? on Fri, 12 Oct 2012 07:49:34 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich bin noch Anfänger und habe das erste Mal was von Exceptions gelesen und habe dazu eine Frage: Kann ich eine std::bad_alloc Exception per Hand auslösen, um das Verhalten, das bei einem Fehler bei new auftritt, zu simulieren?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2259521</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2259521</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Fri, 12 Oct 2012 07:49:34 GMT</pubDate></item><item><title><![CDATA[Reply to std::bad_alloc Exception per Hand auslösen? on Fri, 12 Oct 2012 07:52:48 GMT]]></title><description><![CDATA[<p>Grasshopper schrieb:</p>
<blockquote>
<p>Kann ich eine std::bad_alloc Exception per Hand auslösen, um das Verhalten, das bei einem Fehler bei new auftritt, zu simulieren?</p>
</blockquote>
<p>Jein. Du kannst eine bad_alloc Exception natürlich auch manuell werfen, wie jede andere Exception auch. Allerdings würde ich das in realem Code nie machen, das verwirrt nur <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>Die Frage ist, was willst du damit (mit der Simulation) erreichen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2259524</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2259524</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Fri, 12 Oct 2012 07:52:48 GMT</pubDate></item><item><title><![CDATA[Reply to std::bad_alloc Exception per Hand auslösen? on Fri, 12 Oct 2012 08:49:40 GMT]]></title><description><![CDATA[<p>Hmm, na ich will mal eine Instanz einer KLasse anlegen und dann so tun als könnte die Klasse nicht per new erstellt werden. Das will ich mal versuchen um zu testen ob ich da auch richtig mit den Execption umgehen kann und es verstanden haben.</p>
<p>Ich habe jetzt mal ein kleines Programm geschrieben, könnte mir einer sagen ob ich da richtig mit Exceptions umgehen? Ich bin noch Anfänger also bitte den Quellcode nicht zu sehr fertig machen <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>
<pre><code class="language-cpp">#include &lt;iostream&gt;

using namespace std;

class MyException 
{
public:
    void doSomething()
    {
        cout &lt;&lt; &quot;Object from class MyEecption::doSomething()&quot; &lt;&lt; endl;
    }
};

bool doLoop;

void exceptionThrower()
{
    cout &lt;&lt; &quot;=================&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;Exception Thrower&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;=================&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;1.) int 10&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;2.) char 'a'&quot;&lt;&lt; endl;
    cout &lt;&lt; &quot;3.) MyException class&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;0.) End program&quot;&lt;&lt; endl;
    cout &lt;&lt; endl;
    cout &lt;&lt; &quot;Please make your choice:&quot;;
    int c = 0;
    cin &gt;&gt; c;

    int a = 10;
    char b ='a';
    MyException my;

    switch(c)
    {
    case 0:
        doLoop = false;
        break;

    case 1:
        cout &lt;&lt; &quot;Will throw Exception int 10&quot; &lt;&lt; endl;
        throw a;
        break;

    case 2:
        cout &lt;&lt; &quot;Will throw Exception char 'a'&quot; &lt;&lt; endl;
        throw b;
        break;

    case 3:
        cout &lt;&lt; &quot;Will throw Execption class MyException&quot; &lt;&lt; endl;
        throw my;
        break;

    default:
        cout &lt;&lt; &quot;Wrong input, please try again&quot; &lt;&lt; endl;

    }
}

int main()
{

    doLoop = true;
    while (doLoop)
    {
        try
        {
             exceptionThrower();
        }
        catch(int){ cout &lt;&lt; &quot;Exception int catched&quot; &lt;&lt; endl; }
        catch(char){ cout &lt;&lt; &quot;Exception char catched&quot; &lt;&lt; endl; }
        catch(MyException e)
        { 
            cout &lt;&lt; &quot;Exception MyException catched&quot; &lt;&lt; endl; 
            e.doSomething();
        }
        catch(...){ cout &lt;&lt; &quot;Unknown exception catched&quot; &lt;&lt; endl; }
    }

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2259539</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2259539</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Fri, 12 Oct 2012 08:49:40 GMT</pubDate></item><item><title><![CDATA[Reply to std::bad_alloc Exception per Hand auslösen? on Fri, 12 Oct 2012 16:22:31 GMT]]></title><description><![CDATA[<p>Man leitet Ausnahmen üblicherweise direkt oder indirekt von <code>std::exception</code> ab, um eine einheitliche Ausgabe mit der <code>what()</code> -Methode zu ermöglichen.<br />
Gefangen werden sollten Ausnahmen per ( <code>const</code> -)Referenz, um auch abgeleitete Ausnahmen mitzubekommen.<br />
Etwas, das nicht von <code>std::exception</code> abgeleitet ist, sollte niemals geworfen werden. Dass es überhaupt möglich ist, nicht-polymorphe Objekte zu werfen, ist ein Design-Fehler von C++.</p>
<p>Die globale Variable ist unnötig. Du könntest die Abbruchbedingung über den Rückgabewert von <code>exceptionThrower</code> zurückgeben.<br />
&quot;catched&quot; ist kein englisches Wort.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2259743</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2259743</guid><dc:creator><![CDATA[TyRoXx]]></dc:creator><pubDate>Fri, 12 Oct 2012 16:22:31 GMT</pubDate></item><item><title><![CDATA[Reply to std::bad_alloc Exception per Hand auslösen? on Sat, 13 Oct 2012 02:57:41 GMT]]></title><description><![CDATA[<p>Danke für die Tipps. Ich habe es etwas modifiziert, so besser?</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

using namespace std;

class MyException : public std::exception
{
public:
    const char* what() const throw()
    {
        return &quot;Object from class MyExecption::what()&quot;;
    }
};

bool exceptionThrower()
{
    cout &lt;&lt; &quot;=================&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;Exception Thrower&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;=================&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;1.) int 10&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;2.) char 'a'&quot;&lt;&lt; endl;
    cout &lt;&lt; &quot;3.) MyException class&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;0.) End program&quot;&lt;&lt; endl;
    cout &lt;&lt; endl;
    cout &lt;&lt; &quot;Please make your choice:&quot;;
    int c = 0;
    cin &gt;&gt; c;

    int a = 10;
    char b ='a';
    MyException my;

    bool nextTurn = true;

    switch(c)
    {
    case 0:
        nextTurn = false;
        break;

    case 1:

        cout &lt;&lt; &quot;Will throw Exception int 10&quot; &lt;&lt; endl;
        throw a;
        break;

    case 2:
        cout &lt;&lt; &quot;Will throw Exception char 'a'&quot; &lt;&lt; endl;
        throw b;
        break;

    case 3:
        cout &lt;&lt; &quot;Will throw Execption class MyException&quot; &lt;&lt; endl;
        throw my;
        break;

    default:
        cout &lt;&lt; &quot;Wrong input, please try again&quot; &lt;&lt; endl;

    }
    return nextTurn;
}

int main()
{
    bool doLoop = true;

    while (doLoop)
    {
        try
        {
            doLoop = exceptionThrower();
        }
        catch(int){ cout &lt;&lt; &quot;Exception int caught&quot; &lt;&lt; endl; }
        catch(char){ cout &lt;&lt; &quot;Exception char caught&quot; &lt;&lt; endl; }
        catch(std::exception&amp; e)
        { 
            cout &lt;&lt; &quot;Exception MyException caught&quot; &lt;&lt; endl; 
            cout &lt;&lt; e.what() &lt;&lt; endl;
        }
        catch(...){ cout &lt;&lt; &quot;Unknown exception caught&quot; &lt;&lt; endl; }
    }

    return 0;
}
</code></pre>
<p>EDIT:Englisch verbessert, da bin ich noch mieser als in C++.^^</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2259764</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2259764</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sat, 13 Oct 2012 02:57:41 GMT</pubDate></item><item><title><![CDATA[Reply to std::bad_alloc Exception per Hand auslösen? on Fri, 12 Oct 2012 18:01:36 GMT]]></title><description><![CDATA[<p>Grasshopper schrieb:</p>
<blockquote>
<p>captured</p>
</blockquote>
<p><a href="http://de.bab.la/konjugieren/englisch/catch" rel="nofollow">http://de.bab.la/konjugieren/englisch/catch</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2259774</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2259774</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Fri, 12 Oct 2012 18:01:36 GMT</pubDate></item><item><title><![CDATA[Reply to std::bad_alloc Exception per Hand auslösen? on Fri, 12 Oct 2012 18:37:13 GMT]]></title><description><![CDATA[<p>Tut mir Leid, mein Englisch ist lange her. Lesen geht ganz gut, aber schreiben ist unter aller Sau.</p>
<p>Welche Form nehme ich hier und warum: Simple past, Past continuous, Past perfect, Past perfect continuous?</p>
<p>EDIT: Was ist mit dem wichtigem, dem Code?^^</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2259778</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2259778</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Fri, 12 Oct 2012 18:37:13 GMT</pubDate></item><item><title><![CDATA[Reply to std::bad_alloc Exception per Hand auslösen? on Fri, 12 Oct 2012 20:53:17 GMT]]></title><description><![CDATA[<p>Simple past: caught.</p>
<p>Grasshopper schrieb:</p>
<blockquote>
<p>Was ist mit dem wichtigem, dem Code?^^</p>
</blockquote>
<p>jo, zun ausprobieren passt's scho'.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2259821</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2259821</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Fri, 12 Oct 2012 20:53:17 GMT</pubDate></item><item><title><![CDATA[Reply to std::bad_alloc Exception per Hand auslösen? on Sat, 13 Oct 2012 08:09:11 GMT]]></title><description><![CDATA[<p>Damit es zum Thema passt, habe ich noch selbst eine std::bad_alloc geworfen, so wie dies auch bei einem Fehlschlagen von new eintreten würde. Zudem wurde noch der Text beim Fangen von std::execption geändert, da ja hier nicht nur meine Fehlerklassen gefangen wird, sondern alle Klassen die von std::execption abgeleitet sind, also auch std::bad_alloc.</p>
<p>Vielleicht hilft es ja den ein oder anderen auch beim Einstieg in die Welt der Ausnahmen.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

using namespace std;

class MyException : public std::exception
{
public:
    const char* what() const throw()
    {
        return &quot;Object from class MyExecption::what()&quot;;
    }
};

bool exceptionThrower()
{
    cout &lt;&lt; &quot;=================&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;Exception Thrower&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;=================&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;1.) int 10&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;2.) char 'a'&quot;&lt;&lt; endl;
    cout &lt;&lt; &quot;3.) MyException class&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;4.) std::bad_alloc&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;0.) End program&quot;&lt;&lt; endl;
    cout &lt;&lt; endl;
    cout &lt;&lt; &quot;Please make your choice:&quot;;
    int c = 0;
    cin &gt;&gt; c;

    int a = 10;
    char b ='a';
    MyException my;

    bool nextTurn = true;

    switch(c)
    {
    case 0:
        nextTurn = false;
        break;

    case 1:
        cout &lt;&lt; &quot;Will throw Exception int 10&quot; &lt;&lt; endl;
        throw a;
        break;

    case 2:
        cout &lt;&lt; &quot;Will throw Exception char 'a'&quot; &lt;&lt; endl;
        throw b;
        break;

    case 3:
        cout &lt;&lt; &quot;Will throw Exception class MyException&quot; &lt;&lt; endl;
        throw my;
        break;

    case 4:
        cout &lt;&lt; &quot;Will throw Exception std::bad_alloc&quot; &lt;&lt; endl;
        throw std::bad_alloc();
        break;

    default:
        cout &lt;&lt; &quot;Wrong input, please try again&quot; &lt;&lt; endl;

    }
    return nextTurn;
}

int main()
{
    bool doLoop = true;

    while (doLoop)
    {
        try
        {
            doLoop = exceptionThrower();
        }
        catch(int){ cout &lt;&lt; &quot;Exception int caught&quot; &lt;&lt; endl; }
        catch(char){ cout &lt;&lt; &quot;Exception char caught&quot; &lt;&lt; endl; }
        catch(std::exception&amp; e)
        { 
            cout &lt;&lt; &quot;Exception std::exception caught&quot; &lt;&lt; endl; 
            cout &lt;&lt; e.what() &lt;&lt; endl;
        }
        catch(...){ cout &lt;&lt; &quot;Unknown exception caught&quot; &lt;&lt; endl; }
    }

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2259882</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2259882</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sat, 13 Oct 2012 08:09:11 GMT</pubDate></item></channel></rss>