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

John McCall rjmccall at apple.com
Mon Feb 17 18:50:00 UTC 2014


On Feb 15, 2014, at 11:05 AM, Marshall Clow <mclow.lists at gmail.com> wrote:
> 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 would expect this to be controlled by the language standard, not by the
ABI document.

Amusingly, the current standard is simply ungrammatical:
  N3797 [terminate]p1:
    Called by the implementation when exception handling must be abandoned
    for any of several reasons, in effect immediately after throwing the exception.
Clearly what’s “in effect” is supposed to be the terminate_handler, but...

As far as I can tell, this is a splice left over from a previous revision.  e.g.
  C++03 [lib.terminate]p1:
    Effect: Calls the terminate_handler function in effect immediately after evaluating
    the throw-expression, if called by the implementation, or calls the current
    terminate_handler function, if called by the program.

Both standards are vague about *which* throw-expression is meant when more
than one is associated with the same exception object.  If you can get the
committee to clarify, please ask them to consider what should happen with
std::rethrow_exception as well.

John.


More information about the cxx-abi-dev mailing list