Exeption im Konstuktor von Elternklasse - Frage
-
Hi,
class A { A(int a) { throw std::exception(); } }; class B : public A { B() : A(0) { } }; int main() { B b; return 0; }
Was geschieht wenn der Konstruktor von A() eine Exception wirft? Habe ich dann wirklich keine Möglichkeit die Exception im Konstruktor der Kindsklasse zu fangen und kann das erst in der main() tun?
Grüße
-
Habe ich dann wirklich keine Möglichkeit die Exception im Konstruktor der Kindsklasse zu fangen..?
Doch, das geht...
http://gotw.ca/gotw/066.htm
http://stackoverflow.com/questions/2441009/how-to-catch-the-exception-in-initialization-list
-
sdnjgsgjspd schrieb:
Habe ich dann wirklich keine Möglichkeit die Exception im Konstruktor der Kindsklasse zu fangen und kann das erst in der main() tun?
Keine Chance.
-
theta schrieb:
Doch, das geht...
http://gotw.ca/gotw/066.htm
http://stackoverflow.com/questions/2441009/how-to-catch-the-exception-in-initialization-listGeht schon, aber die Exception wird trotzdem weitergereicht.
#include <iostream> #include<exception> struct A { A(int a) { throw std::exception(); } }; struct B : public A { B() try: A(0) { } catch(std::exception & e) { std::cout << "Cought!\n"; } }; int main(int argc, char * argv[]) { B b; // exception fliegt hier trotzdem! return 0; }
Edit: Man kann aber statt der ursprünglichen auch seine eigene Exception werfen, es muß lediglich irgendetwas fliegen.