Why __cxa_end_catch does not take an argument...

Christophe de Dinechin ddd at cup.hp.com
Fri Dec 17 01:19:38 UTC 1999


Folks,


The reason __cxa_end_catch does not need the exception argument is that the
exceptions it is interested in are in the "caught stack". When you rethrow, the
exception you rethrow is also on this caught stack (it is indeed the top of the
stack). So you don't need a separate copy or argument.

All you need is a flag set by __rethrow, saying "this top exception is the one
being just rethrown". In that case, when __end_catch finds that the exception
exits its last catch block, it will not delete it. Instead, the exception will
just be popped from the stack. As a result, the exception being rethrown remains
on the caught stack until you exit the last catch that caught it, and then
becomes referred to only through the exception object passed in the runtime
(that is, it becomes similar to a new exception being thrown: it does not appear
in the caught stack.) This is the "stack + 1" model I mentioned...

__begin_catch clears the flag, in case you catch the rethrown exception before
exiting the last catch handler.

This mechanism is actually correctly specified in the description of
__cxa_end_catch (see in particular the last bullet):

Upon exit from the handler by any means, the epilogue calls __cxa_end_catch(),
which:
- Locates the most recently caught exception and decrements its handler count.
- Removes the exception from the "caught" stack if the handler count goes to
zero.
- Destroys the exception if the handler count goes to zero, and the exception
was not re-thrown by throw;.

What is unclear, though, is the fact that __rethrow needs to pass a flag to
__end_catch for that purpose, and also that the flag is stored in the high bit
of the handlerCount (which is why it did not appear in the specification...).
And, by the way, looking back at that code made me find a bug in my current
implementation, thanks :-)

More editing ahead...


Best regards
Christophe




More information about the cxx-abi-dev mailing list