public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/17335] New: rethrowing exceptions and capturing by reference
@ 2004-09-06 13:40 staube at t2 dot technion dot ac dot il
  2004-09-06 17:06 ` [Bug c++/17335] " giovannibajo at libero dot it
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: staube at t2 dot technion dot ac dot il @ 2004-09-06 13:40 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 3278 bytes --]

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2004-09-07 23:51 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-06 13:40 [Bug c++/17335] New: rethrowing exceptions and capturing by reference staube at t2 dot technion dot ac dot il
2004-09-06 17:06 ` [Bug c++/17335] " giovannibajo at libero dot it
2004-09-06 22:13 ` staube at t2 dot technion dot ac dot il
2004-09-07  5:24 ` jason at redhat dot com
2004-09-07  6:31 ` staube at t2 dot technion dot ac dot il
2004-09-07 20:13 ` jason at redhat dot com
2004-09-07 23:51 ` bangerth at dealii dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).