[Bug c++/17335] New: rethrowing exceptions and capturing by reference
staube at t2 dot technion dot ac dot il
gcc-bugzilla@gcc.gnu.org
Mon Sep 6 13:40:00 GMT 2004
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
--
Summary: rethrowing exceptions and capturing by reference
Product: gcc
Version: 3.3.3
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: staube at t2 dot technion dot ac dot il
CC: gcc-bugs at gcc dot gnu dot org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17335
More information about the Gcc-bugs
mailing list