Try catch



  • Hallo,

    ich habe einen änfänger Frage:
    wenn ich so was habe:

    try
    {
    fkt1();
    fkt2();
    fk3();
    ....
    
    }
    catch(..)
    {
    ....
    }
    

    und bei der fkt1() einen Error auftritt, werden die Funktionen (fkt2()und fkt3()) trotzdem weiter ausgeführt oder wird das Programm direkt in der Catch Block springen?

    Danke 😕



  • Gehe in den catch-Block. Begieb Dich direkt dort hin, führe nicht fkt2 und fkt3 aus.



  • Folgendes Problem, für das ich im Internet keine Lösung gefunden habe,
    Beispielprogramm:

    #include <stdio.h>
    #include <exception>

    class Exceptionhandling
    {
    public:
    Exceptionhandling(long line)
    : lineNr (line)
    {}

    long GetLine() {
    return lineNr;
    }

    private:
    long lineNr;
    };

    class CTest
    {
    public:

    CTest (int data_)
    : data(data_)
    {}
    int GetData() {
    return data;
    }
    private:
    int data;
    };

    int main(int argc, char* argv[])
    {
    int result (0);
    int line (__LINE__);

    try {
    int xx (3);
    CTest test (xx);
    CTest (*pTest) (&test);
    pTest = (CTest 😉 0L; // Fehlerfall erzeugen

    result = pTest->GetData();
    }

    /*
    catch (<DatenTyp der Exception> & nullPtr) {
    fprintf (stdout, "Exception in Line %ld\n", result);
    }
    */

    catch (Exceptionhandling &ex) {
    result = ex.GetLine();
    fprintf (stdout, "Exception in Line %ld\n", result);
    }

    catch (CException &ex) {
    fprintf (stdout, "CException \n");
    }

    catch (...) {
    result = -1;
    }

    return result;
    }

    Im obigen Programm wird in der Zeile
    result = pTest->GetData();
    eine Exception geworfen, weil pText in der Zeile vorher auf 0L gesetzt wurde.

    Ich möchte diese Exception mit
    catch ( <DatenTyp der Exception> )
    fangen.

    Was muß für <DatenTyp der Exception> stehen?

    Im vorraus besten Dank für die Hilfe.

    Gruß
    Hans



  • Warum machst du keinen eigenen Thread auf?
    Warum benutzt du keine Codetags?
    Nullpointerdereferenzierungen kann man nicht standardkonform abfangen.


Anmelden zum Antworten