Herzlich Willkommen, lieber Gast!
  Sie befinden sich hier:

  Forum » C / C++ (ANSI-Standard) » Exception ohne Object wird nicht gefangen

Forum | Hilfe | Team | Links | Impressum | > Suche < | Mitglieder | Registrieren | Einloggen
  Quicklinks: MSDN-Online || STL || clib Reference Grundlagen || Literatur || E-Books || Zubehör || > F.A.Q. < || Downloads   

Autor Thread - Seiten: > 1 <
000
03.02.2004, 15:57 Uhr
~Frank
Gast


Hallo.

Habe den gcc3.3.2 unter Linux.
Der folgene Code führt zu einem Abgebrochen auf der Konsole. Das throw wird also nicht in dem catch Block gefangen. Ein throw 1 aber schon.
Was passiert hier???


C++:
#include <iostream>
using namespace std;

int main(void) {
   try{
        throw;
    }
   catch(...){
        cerr << "ex." << endl;
   }

    return 0;
}




Gruß Frank
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
001
03.02.2004, 17:13 Uhr
virtual
Sexiest Bit alive
(Operator)


Ein throw ohne Parameter nennt man re-throw und geht nur dann, wenn Du bereits eine Exception hast. Und das sollte eigentlich nur innerhalb eines catch blockes gehen. Vermutlich kann der gcc die Lage nicht ganz überschauen und erkennt nicht, daß das re-throw außerhalb eines catch blocks liegt. Speziell bei main könnte er das aber theoretisch.
--
Gruß, virtual
Quote of the Month
Ich eß' nur was ein Gesicht hat (Creme 21)
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
002
04.02.2004, 08:55 Uhr
~Frank
Gast


Hab das nochmal mit dem Borland probiert, es passiert das Gleiche.

Also ist wohl der Programmierer schuld, wenn er ein re-throw in einen Try Block schreibt. Könnte aber eigentlich doch der Kompiler meckern, zumindest ne Warnung...

Das re-throw scheint ja im try Block eine nicht fangbare Exception auszulösen, oder was auch immer. Unter Borland kommt dann "abnormal program termination".

mfg Frank
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
003
04.02.2004, 09:05 Uhr
virtual
Sexiest Bit alive
(Operator)


Die einzige Stelle, wo der Compiler wirklich meckern können dürfte (und selbst das ist nicht vom Standard explizit gefordert), ist main. An allen anderen Stellen IMHO nicht. zB:

C++:
#include <iostream>


void func()
{
    try
    {
        throw; // Hier!
    }
    catch(...)
    {
        std::cerr<<"Exception caught"<<std::endl;
    }
}


Wir haben eine Funktion func, die eigentlich exakt dem main entspricht (wemm wir mal von den Parametern absehen).
Diese Funktion stürzt natürlich ab, wenn wir sie achtlos einfach so aufrufen; wenn aber sie stets nur aus einem catch Block heraus aufrufen, macht sie Durchaus sinn:

C++:
int main()
{
    try
    {
        throw 1;
    }
    catch(...)
    {
        func();
    }
}


--
Gruß, virtual
Quote of the Month
Ich eß' nur was ein Gesicht hat (Creme 21)

Dieser Post wurde am 04.02.2004 um 09:06 Uhr von virtual editiert.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
004
04.02.2004, 09:30 Uhr
~Frank
Gast


Nun gut, das macht nur Sinn wenn func() ebenfalls ne Exception werfen kann:


C++:
void func()
{
    try
    {  

        // Code welcher seinerseits ne Exception werfen kann, der gesondert                behandelt werden soll
        throw; // Weiterleiten der gefangenen Exception
    }
    catch(...)
    {
        std::cerr<<"Exception caught"<<std::endl;
        // Behandlung der in func() geworfenen Exception
    }
}



Ich denke mal mir ist das jetzt klar.
F.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
005
04.02.2004, 11:40 Uhr
virtual
Sexiest Bit alive
(Operator)


Ich finde, man kann das schon sinnvoll einsetzen, ohne daß man in der funktion selbst was wirft. Siehe exception_logger

C++:
#include <iostream>
#include <stdexcept>

#define LOG_EXCEPTION(e) exception_logger(__FILE__, __LINE__, (e));


void exception_logger(const char* p_szFilename, unsigned p_nLine, std::exception* p_poException)
{
    std::cerr<<"Exception caught at "<<p_szFilename<<"("<<p_nLine<<")";
    // Ausgabe der Exception nur, wenn von std::exception abgelitten
    if (NULL != p_poException)
    {
        std::cerr<<": "<<p_poException->what();
    }
    std::cerr<<std::endl;
    // Exception aber jedenfalls rethrown
    throw;
}


int teile(int a, int b)
{
    try
    {
        if (b==0) throw std::runtime_error("division_by_zero");
        return a/b;
    }
    catch(std::exception& e)
    {
        LOG_EXCEPTION(&e);
    }
    catch(...)
    {
        LOG_EXCEPTION(NULL);
    }
}


int dreisatz(int a, int b, int c)
{
    try
    {
        return c*teile(a,b);
    }
    catch(std::exception& e)
    {
        LOG_EXCEPTION(&e);
    }
    catch(...)
    {
        LOG_EXCEPTION(NULL);
    }
}


int main()
{
    try
    {
        std::cerr<<"4/0*5="<<dreisatz(4,0,5);
    }
    catch(...)
    {
        std::cerr<<"Something failed. See output where."<<std::endl;
    }
}


--
Gruß, virtual
Quote of the Month
Ich eß' nur was ein Gesicht hat (Creme 21)
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
Seiten: > 1 <     [ C / C++ (ANSI-Standard) ]  


ThWBoard 2.73 FloSoft-Edition
© by Paul Baecher & Felix Gonschorek (www.thwboard.de)

Anpassungen des Forums
© by Flo-Soft (www.flo-soft.de)

Sie sind Besucher: