I have been noticed that gcc calls the copy constructor each time it rethrows an exception. Since i don´t know how exception throwing really works i don´t know if this is necesarry or not, however i think it presents some problems. For example when an exception is captured by reference and rethrown, like in this code: try { doSomething(); } catch (exception &e){ throw e; } This stupid block only catches an exception and rethrows it, the problem is that it catches and exception as an std::exception reference, when it rethrows the exception it wants to call the copy constructor of std::exception, which in fact doesn't exist and even if it would exist it wouldn't correctly copy all the members of e because e has additional data. To test this i made an example programm. This is the code of main.cpp: ///////////////////////code_start///////////////////////////// #include <iostream> using namespace std; class E : public exception{ public: E(const E&) throw(){ cout << "E(const E&)\n"; } E() throw(){ cout << "E()\n"; } ~E() throw(){ cout << "~E()\n"; } const char *what() throw(){ return "this is an exception :-O"; } }; int main(int argc, char *argv[]) { try{ try { throw E(); } catch (exception &e){ throw e; } } catch(exception &e2) { cout << e2.what() << endl; } return 0; } //////////////////////////////////end_code////////////////////////////// This code compiles without any error nor warning, even compiling with the flags -Wall and -pedantic. At least that's true for "gcc version 3.3.3 (mingw special)". What this code should do?? At least in my opinion the code should throw an exception of class E, then catch it and rethrow it and then recatch it and then print the string returned by the what() method. What the code actually does in gcc?? It's difficult to tell, since i know by some other tests that gcc calls the copy constructor of the catched class when rethrowing exceptions i would excpect gcc to show an error message saying that it cannot find the copy constructor of std::exception, even more it should say that an abstract class cannot be constructed because the method what() is pure virtual in std::exception. However it compiles and it even outputs something. This is the output ///////output_start/////// E() ~E() St9exception ///////output_end /////// As it was supposed to be, we see that the E constructor is called, then destroyed. The copy constructor of E is never called, i cannot imagine then, which copy constructor is called. The result of the what() function has nothing to do with the defined what() of the class E. Am i making a mistake, writing an invalid C++ programm or is this actually a bug in gcc. Thanks for the help
Jason, what is your opinion on this?
I have thinked about an additional problem of using the copy constructor for rethrowing (i have just thought about it). If you use the copy constructor for the base class then you will never be able to recatch the exception with a catch for the derived class. In general using the copy constructor of the base class means object-slicing just as if the exception was catched by value and not by reference. Thanks again Marcelo
Subject: Re: rethrowing exceptions and capturing by reference If you want to rethrow the current exception without copying, write throw; This is not a bug. Jason
jason, you are right, i forgot that. But anyway the compiler used an unexistant copy constructor and builded an object for an ABSTRACT class without issiuing any warnings nor errors. That has to be an error, right?
Subject: Re: rethrowing exceptions and capturing by reference On 7 Sep 2004 06:31:02 -0000, "staube at t2 dot technion dot ac dot il" <gcc-bugzilla@gcc.gnu.org> wrote: > But anyway the compiler used an unexistant copy constructor and builded > an object for an ABSTRACT class without issiuing any warnings nor > errors. That has to be an error, right? The copy constructor for class exception exists, it's just trivial. class exception is not abstract. exception::what is not pure virtual. Jason
Just as Jason says: use "throw;"