Bug 17335 - rethrowing exceptions and capturing by reference
Summary: rethrowing exceptions and capturing by reference
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.3.3
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-09-06 13:39 UTC by Marcelo Taube
Modified: 2005-07-23 22:49 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Marcelo Taube 2004-09-06 13:39:56 UTC
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
Comment 1 Giovanni Bajo 2004-09-06 17:06:43 UTC
Jason, what is your opinion on this?
Comment 2 Marcelo Taube 2004-09-06 22:13:12 UTC
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
Comment 3 Jason Merrill 2004-09-07 05:24:09 UTC
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
Comment 4 Marcelo Taube 2004-09-07 06:31:00 UTC
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?
Comment 5 Jason Merrill 2004-09-07 20:13:34 UTC
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
Comment 6 Wolfgang Bangerth 2004-09-07 23:51:43 UTC
Just as Jason says: use "throw;"