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

* [Bug c++/17335] rethrowing exceptions and capturing by reference
  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 ` giovannibajo at libero dot it
  2004-09-06 22:13 ` staube at t2 dot technion dot ac dot il
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: giovannibajo at libero dot it @ 2004-09-06 17:06 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From giovannibajo at libero dot it  2004-09-06 17:06 -------
Jason, what is your opinion on this?

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jason at redhat dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17335


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

* [Bug c++/17335] rethrowing exceptions and capturing by reference
  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
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: staube at t2 dot technion dot ac dot il @ 2004-09-06 22:13 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From staube at t2 dot technion dot ac dot il  2004-09-06 22:13 -------
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

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17335


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

* [Bug c++/17335] rethrowing exceptions and capturing by reference
  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
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: jason at redhat dot com @ 2004-09-07  5:24 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From jason at redhat dot com  2004-09-07 05:24 -------
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


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17335


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

* [Bug c++/17335] rethrowing exceptions and capturing by reference
  2004-09-06 13:40 [Bug c++/17335] New: rethrowing exceptions and capturing by reference staube at t2 dot technion dot ac dot il
                   ` (2 preceding siblings ...)
  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
  5 siblings, 0 replies; 7+ messages in thread
From: staube at t2 dot technion dot ac dot il @ 2004-09-07  6:31 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From staube at t2 dot technion dot ac dot il  2004-09-07 06:31 -------
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?

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17335


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

* [Bug c++/17335] rethrowing exceptions and capturing by reference
  2004-09-06 13:40 [Bug c++/17335] New: rethrowing exceptions and capturing by reference staube at t2 dot technion dot ac dot il
                   ` (3 preceding siblings ...)
  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
  5 siblings, 0 replies; 7+ messages in thread
From: jason at redhat dot com @ 2004-09-07 20:13 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From jason at redhat dot com  2004-09-07 20:13 -------
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


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17335


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

* [Bug c++/17335] rethrowing exceptions and capturing by reference
  2004-09-06 13:40 [Bug c++/17335] New: rethrowing exceptions and capturing by reference staube at t2 dot technion dot ac dot il
                   ` (4 preceding siblings ...)
  2004-09-07 20:13 ` jason at redhat dot com
@ 2004-09-07 23:51 ` bangerth at dealii dot org
  5 siblings, 0 replies; 7+ messages in thread
From: bangerth at dealii dot org @ 2004-09-07 23:51 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2004-09-07 23:51 -------
Just as Jason says: use "throw;" 

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


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).