[cxx-abi-dev] A question about terminate handlers

Marshall Clow mclow.lists at gmail.com
Sat Feb 15 19:05:42 UTC 2014


Consider the following code (example from Stephen T. Lajavev)

#include <stdio.h>
#include <stdlib.h>
#include <exception>
using namespace std;

void MyTerminate() {
    puts("MyTerminate()");
    abort();
}

int main() {
    try {
        try {
            puts("main() throwing int");
            throw 5;
        } catch (int) {
            puts("int caught initially");
            #ifdef MEOW
                puts("calling set_terminate()");
                set_terminate(MyTerminate);
            #endif
            puts("rethrowing");
            throw;
        }
    } catch (int) {
        puts("int caught again, calling terminate()");
        terminate();
    }
}

[ Tests on Mac OS X Mavericks  - g++11 is an alias for “g++-4.8.2 -std=c++11” ]

$ clang++ -std=c++11 -stdlib=libc++ -DMEOW meow.cpp && ./a.out
main() throwing int
int caught initially
calling set_terminate()
rethrowing
int caught again, calling terminate()
libc++abi.dylib: terminating with uncaught exception of type int
Abort trap: 6
$ g++11 meow.cpp -DMEOW && ./a.out
main() throwing int
int caught initially
calling set_terminate()
rethrowing
int caught again, calling terminate()
MyTerminate()
Abort trap: 6
$ 

Specifically:
	libc++ & libc++abi call the terminate handler that was active when the exception was created.
	libstdc++ and libsup++ call the terminate handler that was installed when the exception was rethrown.

Does the C++ ABI document have anything to say about terminate handlers being updated while an exception is in flight?
[ I wonder if __cxa_rethrow should reload the saved terminate (and maybe unexpected) handlers in the exception object ]

— Marshall





More information about the cxx-abi-dev mailing list