[Gc] workaround for C++ exceptions problem

Filip Pizlo pizlo at mac.com
Tue Jan 24 19:20:46 PST 2006


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 program  
to see what I'm talking about:

#include <iostream>
using namespace std;
struct C {
    ~C() {
       throw 42;
    }
};
int main() {
    try {
       C c;
       throw 5;
    } catch (...) {
       cout<<"ok!"<<endl;
    }
    return 0;
}

The program should get a SIGABRT, since this is the signal raised by  
the default terminate() handler.

However, you are right that multiple exceptions can be in flight at  
once.  This happens when you have try blocks nested in catch blocks,  
like:

try {
   throw 24;
} catch (...) {
   try {
     throw 42;
   } catch (...) {
     // here we have two exceptions in flight: 24 and 42
   }
}

-Filip



>
> Observe this will unwind the stack of the destructor,
> which calls destructors of locals constructed by
> the destructor .. which in turn can do the same
> thing.
>
> This, any number of scopes can be in the process
> of being unwound at any one time, and any number
> of exceptions can be 'in flight' in the sense
> they haven't been caught yet.
>
> -- 
> John Skaller <skaller at users dot sf dot net>
> Felix, successor to C++: http://felix.sf.net
>



More information about the Gc mailing list