[Gc] workaround for C++ exceptions problem

skaller skaller at users.sourceforge.net
Tue Jan 24 19:38:12 PST 2006


On Tue, 2006-01-24 at 22:20 -0500, Filip Pizlo wrote:
> On Jan 24, 2006, at 10:06 PM, skaller wrote:
> 
> > On Tue, 2006-01-24 at 14:51 -0800, Boehm, Hans wrote:
> >
> >> Would it be much of a problem to restrict clients to one in-flight
> >> exception at a time, per thread?
> >
> > What do you mean by 'inflight'?
> >
> > Any number of exceptions can be 'thrown but not caught'
> > at once, provided the scopes of the stack
> > parts being unwound do not overlap.
> >
> > In particular, in the process of unwinding the stack,
> > destructors for objects on the stack are called.
> > Whilst in the dynamic scope of such a destructor, it is
> > quite reasonable to throw an exception which is caught
> > inside the destructor dynamic context.
> 
> No.  If you are in a destructor triggered by unwinding and you throw  
> an exception, this leads to a call to terminate(). 

Try this to see what I mean:

struct C {
  ~C() { throw 42; }
};

struct D {
  ~D() {
   try {
     C c;
   } catch (...) {}
};

int main() {
    try {
       D d;
       throw 5;
    } catch (...) {
       cout<<"ok!"<<endl;
    }
    return 0;
}

In main, D is unwound by throwing 5. This invokes
the destructor of C which throws 42.

At this point BOTH 5 and 42 are 'in flight':
neither has been caught. terminate() is NOT called.

If it is its a bug in your C++ implementation.

-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net



More information about the Gc mailing list