Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Trouble with C++ exceptions, code



At 11:47 +0200 7/28/03, Bjvrn Giesler wrote:
I'm trying to catch an exception using a set_terminate handler, which
re-throws the exception and tries to catch it using a number of
templates, including "...". On Linux, this works fine; on OS X, the
handler gets stuck in an endless recursion, re-throwing the exception
and being called over and over again.

I've attached the problematic isolated code; the output is as follows:

void uncaughtHandler(void)
{
try {
printf("Re-throwing.\n");
throw;
} catch(MyException& e) {
printf("MyException\n");
abort();
} catch(...) {
printf("Unknown\n");
abort();
}
}

On OSX 10.2.6, gcc 3.1 20020420 (prerelease):
$ gcc -o testprogram Main.cc -lstdc++
$ ./testprogram
Re-throwing.
Re-throwing.
Re-throwing.
[... etc until the stack overflows]

On Linux 2.4.19, gcc 3.2:
$ gcc -o testprogram Main.cc -lstdc++
$ ./testprogram
Re-throwing.
MyException
Aborted

It looks like gcc is not calling set-terminate from outside an exception context, meaning the "try" case is being hit, which does a throw with no pending exception, which probably results in a different exception, rinse, lather, repeat...

Also, your uncaughtHandler() handler will choke in the manner you describe if terminate() is called for any reason _except_ someone throwing MyException, so you definitely have some work to do on your test code.

As a first approximation:

void terminateHandler(void)
{
try {
printf("terminate() called.\n");
call_old_terminate();
} catch(MyException& e) {
printf("MyException\n");
call_old_terminate();
} catch(...) {
printf("Unknown\n");
call_old_terminate();
}
}

where call_old_terminate() calls the next terminate handler in the stack as returned from set_terminate(). It also sounds like you should be using set_unexpected().

There's also an interesting question in here about how gcc handles exceptions that I'd like to hear the discussion of.

HTH,

-Steve
_______________________________________________
darwin-development mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/darwin-development
Do not post admin requests to the list. They will be ignored.

References: 
 >Trouble with C++ exceptions, code (From: Björn Giesler <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.