std::bad_alloc Exception per Hand auslösen?



  • Hallo,

    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?



  • Grasshopper schrieb:

    Kann ich eine std::bad_alloc Exception per Hand auslösen, um das Verhalten, das bei einem Fehler bei new auftritt, zu simulieren?

    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 😉

    Die Frage ist, was willst du damit (mit der Simulation) erreichen?



  • 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.

    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 🙂

    #include <iostream>
    
    using namespace std;
    
    class MyException 
    {
    public:
        void doSomething()
        {
            cout << "Object from class MyEecption::doSomething()" << endl;
        }
    };
    
    bool doLoop;
    
    void exceptionThrower()
    {
        cout << "=================" << endl;
        cout << "Exception Thrower" << endl;
        cout << "=================" << endl;
        cout << "1.) int 10" << endl;
        cout << "2.) char 'a'"<< endl;
        cout << "3.) MyException class" << endl;
        cout << "0.) End program"<< endl;
        cout << endl;
        cout << "Please make your choice:";
        int c = 0;
        cin >> c;
    
        int a = 10;
        char b ='a';
        MyException my;
    
        switch(c)
        {
        case 0:
            doLoop = false;
            break;
    
        case 1:
            cout << "Will throw Exception int 10" << endl;
            throw a;
            break;
    
        case 2:
            cout << "Will throw Exception char 'a'" << endl;
            throw b;
            break;
    
        case 3:
            cout << "Will throw Execption class MyException" << endl;
            throw my;
            break;
    
        default:
            cout << "Wrong input, please try again" << endl;
    
        }
    }
    
    int main()
    {
    
        doLoop = true;
        while (doLoop)
        {
            try
            {
                 exceptionThrower();
            }
            catch(int){ cout << "Exception int catched" << endl; }
            catch(char){ cout << "Exception char catched" << endl; }
            catch(MyException e)
            { 
                cout << "Exception MyException catched" << endl; 
                e.doSomething();
            }
            catch(...){ cout << "Unknown exception catched" << endl; }
        }
    
        return 0;
    }
    


  • Man leitet Ausnahmen üblicherweise direkt oder indirekt von std::exception ab, um eine einheitliche Ausgabe mit der what() -Methode zu ermöglichen.
    Gefangen werden sollten Ausnahmen per ( const -)Referenz, um auch abgeleitete Ausnahmen mitzubekommen.
    Etwas, das nicht von std::exception abgeleitet ist, sollte niemals geworfen werden. Dass es überhaupt möglich ist, nicht-polymorphe Objekte zu werfen, ist ein Design-Fehler von C++.

    Die globale Variable ist unnötig. Du könntest die Abbruchbedingung über den Rückgabewert von exceptionThrower zurückgeben.
    "catched" ist kein englisches Wort.



  • Danke für die Tipps. Ich habe es etwas modifiziert, so besser?

    #include <iostream>
    
    using namespace std;
    
    class MyException : public std::exception
    {
    public:
        const char* what() const throw()
        {
            return "Object from class MyExecption::what()";
        }
    };
    
    bool exceptionThrower()
    {
        cout << "=================" << endl;
        cout << "Exception Thrower" << endl;
        cout << "=================" << endl;
        cout << "1.) int 10" << endl;
        cout << "2.) char 'a'"<< endl;
        cout << "3.) MyException class" << endl;
        cout << "0.) End program"<< endl;
        cout << endl;
        cout << "Please make your choice:";
        int c = 0;
        cin >> c;
    
        int a = 10;
        char b ='a';
        MyException my;
    
        bool nextTurn = true;
    
        switch(c)
        {
        case 0:
            nextTurn = false;
            break;
    
        case 1:
    
            cout << "Will throw Exception int 10" << endl;
            throw a;
            break;
    
        case 2:
            cout << "Will throw Exception char 'a'" << endl;
            throw b;
            break;
    
        case 3:
            cout << "Will throw Execption class MyException" << endl;
            throw my;
            break;
    
        default:
            cout << "Wrong input, please try again" << endl;
    
        }
        return nextTurn;
    }
    
    int main()
    {
        bool doLoop = true;
    
        while (doLoop)
        {
            try
            {
                doLoop = exceptionThrower();
            }
            catch(int){ cout << "Exception int caught" << endl; }
            catch(char){ cout << "Exception char caught" << endl; }
            catch(std::exception& e)
            { 
                cout << "Exception MyException caught" << endl; 
                cout << e.what() << endl;
            }
            catch(...){ cout << "Unknown exception caught" << endl; }
        }
    
        return 0;
    }
    

    EDIT:Englisch verbessert, da bin ich noch mieser als in C++.^^



  • Grasshopper schrieb:

    captured

    http://de.bab.la/konjugieren/englisch/catch



  • Tut mir Leid, mein Englisch ist lange her. Lesen geht ganz gut, aber schreiben ist unter aller Sau.

    Welche Form nehme ich hier und warum: Simple past, Past continuous, Past perfect, Past perfect continuous?

    EDIT: Was ist mit dem wichtigem, dem Code?^^



  • Simple past: caught.

    Grasshopper schrieb:

    Was ist mit dem wichtigem, dem Code?^^

    jo, zun ausprobieren passt's scho'.



  • 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.

    Vielleicht hilft es ja den ein oder anderen auch beim Einstieg in die Welt der Ausnahmen.

    #include <iostream>
    
    using namespace std;
    
    class MyException : public std::exception
    {
    public:
        const char* what() const throw()
        {
            return "Object from class MyExecption::what()";
        }
    };
    
    bool exceptionThrower()
    {
        cout << "=================" << endl;
        cout << "Exception Thrower" << endl;
        cout << "=================" << endl;
        cout << "1.) int 10" << endl;
        cout << "2.) char 'a'"<< endl;
        cout << "3.) MyException class" << endl;
        cout << "4.) std::bad_alloc" << endl;
        cout << "0.) End program"<< endl;
        cout << endl;
        cout << "Please make your choice:";
        int c = 0;
        cin >> c;
    
        int a = 10;
        char b ='a';
        MyException my;
    
        bool nextTurn = true;
    
        switch(c)
        {
        case 0:
            nextTurn = false;
            break;
    
        case 1:
            cout << "Will throw Exception int 10" << endl;
            throw a;
            break;
    
        case 2:
            cout << "Will throw Exception char 'a'" << endl;
            throw b;
            break;
    
        case 3:
            cout << "Will throw Exception class MyException" << endl;
            throw my;
            break;
    
        case 4:
            cout << "Will throw Exception std::bad_alloc" << endl;
            throw std::bad_alloc();
            break;
    
        default:
            cout << "Wrong input, please try again" << endl;
    
        }
        return nextTurn;
    }
    
    int main()
    {
        bool doLoop = true;
    
        while (doLoop)
        {
            try
            {
                doLoop = exceptionThrower();
            }
            catch(int){ cout << "Exception int caught" << endl; }
            catch(char){ cout << "Exception char caught" << endl; }
            catch(std::exception& e)
            { 
                cout << "Exception std::exception caught" << endl; 
                cout << e.what() << endl;
            }
            catch(...){ cout << "Unknown exception caught" << endl; }
        }
    
        return 0;
    }
    

Anmelden zum Antworten