On 03/08/2013 11:26 AM, Rob T wrote:
On Friday, 8 March 2013 at 17:40:38 UTC, Ali Çehreli wrote:
On 03/08/2013 12:39 AM, Rob T wrote:
> In C++ you can do this
>
> std::exception Trace()
> {
> try
> {
> // key item missing from D
> throw; // <= rethrow last exception

This idiom is know as a Lippincott Function.

Ali

I'm having trouble finding references on Lippincott Function, so if you
have any more information, please let me know. Thanks.

--rt

I heard about this idiom for the first time in Jon Kalb's talk. The name appears in his slides:

  http://exceptionsafecode.com/

Very smart way of writing a single function that catches many different types of exceptions; does special things about them, like producing special error codes, and then returns those codes to the C world.

I am familiar with your use case: When a C layer calls C++, the exceptions must be caught and converted to error codes. This is how we do it:

#define BEGIN_C_INTERFACE try {

#define END_C_INTERFACE                                     \
} catch (const SomeType & e) {                              \
  // ... special code for SomeType                          \
} catch (const SomeOtherType & e) {                         \
  // ... special code for SomeOtherType                     \
                                                            \
// ... etc.                                                 \
                                                            \
} catch( ... ) {                                            \
    log("Unhandled exception");                             \
    return some_generic_error_code;                         \
}

Then our C++ functions that are called from C are like this:

extern "C"
int my_api_func()
{
    BEGIN_C_INTERFACE

    // ... the actual body of the function

    END_C_INTERFACE
}

Lippincott functions avoid macros and make it more explicit that the entire body is inside a try block.

Ali

Reply via email to