Friday, December 13, 2013

Using a Lippincott Function for Centralized Exception Handling

The lippincott function is a way to wrap the handling of many different exceptions into a single reusable function.

Consider this use case:
  • A C++ library (say, libfoo) has many distinct exception types.
  • You want to wrap the C++ library with a C API.
  • You want error codes instead of exceptions for the C API.

A simple approach is to write every C API function as follows:
typedef enum foo_Result {
    FOO_OK,
    FOO_ERROR1,
    FOO_ERROR2,
    FOO_UNKNOWN
} foo_Result;

foo_Result foo_dothing()
{
    try
    {
        // Can throw MyException1 or MyException2
        foo::DoThing();
    }
    catch (const MyException1&)
    {
        return FOO_ERROR1;
    }
    catch (const MyException2&)
    {
        return FOO_ERROR2;
    }
    catch (...)
    {
        return FOO_UNKNOWN;
    }

    return FOO_OK;
}
There are some maintenance problems with the above approach:
  • What if you update DoThing() to also throw MyException3?
  • You must repeat the exception handling code for every API function.

Jon Kalb suggests the following refactoring method, which is named after Lisa Lippincott, who taught him the technique.
foo_Result lippincott()
{
    try
    {
        throw;
    }
    catch (const MyException1&)
    {
        return FOO_ERROR1;
    }
    catch (const MyException2&)
    {
        return FOO_ERROR2;
    }
    catch (...)
    {
        return FOO_UNKNOWN;
    }
}

foo_Result foo_dothing()
{
    try
    {
        foo::DoThing();
        return FOO_OK;
    }
    catch (...)
    {
        return lippincott();
    }
}
"throw;", when inside a catch block, simply rethrows the currently caught exception. It is not directly placed in a catch block within the lippincott() function, but it works anyways because it is transitively called from within the catch block of foo_dothing().

There are some important preconditions to calling this lippincott():
  • You cannot call the lippincott function from outside a catch block.
    • "throw;" outside of a catch block will call std::terminate().
  •  lippincott() must be noexcept. No exceptions should escape it.
    • It will leak out of the C API otherwise.

If we want extra safety, we can implement safeguards for the aforementioned preconditions:

To safely handle a violation of the first precondition, we can check that std::current_exception()  is not null previous to doing the "throw;"

To prevent an exception being thrown out of lippincott(), we can wrap the whole body of the function in a try/catch.

Here is the "extra safe"/paranoid version of lippincott():
foo_Result lippincott()
try
{
    try
    {
        if (std::exception_ptr eptr = std::current_exception())
        {
            std::rethrow_exception(eptr);
        }
        else
        {
            return FOO_UNKNOWN;
        }
    }
    catch (const MyException1&)
    {
        return FOO_ERROR1;
    }
    catch (const MyException2&)
    {
        return FOO_ERROR2;
    }
    catch (...)
    {
        return FOO_UNKNOWN;
    }
}
catch (...)
{
    return FOO_UNKNOWN;
}
The C API can now be written entirely in the style of foo_dothing(), which will centralize the error code conversion through the lippincott function.

Another interesting idea is to use the lippincott function to convert exceptions into string representations for debugging. This area is especially in need of the extra try/catch surrounding the function, since allocating dynamic strings can fail.

Full working example: http://ideone.com/m2ZfHN

The lippincott function was popularized by Jon Kalb in his exception-safe coding talks.
You can find his explanation here: youtube link

41 comments:

  1. comments: http://www.reddit.com/r/programming/comments/1ss9j2/c_secrets_using_a_lippincott_function_for/

    ReplyDelete
  2. This is an inferior solution compared to simply using a lambda. http://coliru.stacked-crooked.com/a/fce340725ab31872
    Only useful if you don't have access to lambdas.

    ReplyDelete
    Replies
    1. Thanks for sharing, that's a neat way to write your bindings. However, it doesn't quite solve the same problem as the Lippincott function.

      The Lippincott function is used as a generic way to recover the type of an exception and apply an operation on it from within a "catch(...)" block.

      Delete
    2. Yes, but that's a completely unnecessary operation.

      Delete
  3. Awesome code!!
    ty for sharing ...
    8-))

    ReplyDelete
  4. Please, do you know if this approuch works well into c++11/g++?

    ReplyDelete
  5. Good one.I appreciate you for sharing this knowledge.Thank you so much for the examples.As a developer in custom software development company namely yiioverflow it provides you the best custom software services.

    ReplyDelete
  6. You won't believe me, but I was planning to write a blog very similar to the one you have posted here. Great work!

    Java Training In Chennai

    AWS Training In Chennai

    ReplyDelete
  7. Do not use `catch(...)` if you intend to terminate on unknown exceptions, and definitely don't use the paranoid wrapper. You should rely on the runtime to give you an uncaught-exception termination, which is better because it does not invalidate the thrower's stack. If `noexcept` fails it's the runtime's fault.

    See https://devblogs.microsoft.com/oldnewthing/20191024-00/?p=103022.

    ReplyDelete
  8. Tamamen Otomatik Sistem ile Siparişleriniz 7 Gün 24 Saat Hızlı ve Sorunsuz Bir Şekilde Tamamlanmaktadır. instagram takipçi satın al ve daha fazlası.

    instagram takipçi satın al

    instagram beğeni satın al

    instagram takipçi satın al

    instagram takipçi satın al

    instagram takipçi satın al

    instagram takipçi satın al

    instagram takipçi satın al

    instagram takipçi satın al

    instagram beğeni satın al

    instagram takipçi satın al

    instagram takipçi satın al

    instagram takipçi satın al

    instagram takipçi satın al

    instagram takipçi satın al


    takipçi satın al

    ucuz takipçi satın al

    tiktok takipçi satın al

    ReplyDelete
  9. Do you think to lead twitter page about C++ secrets? It is very easy to promote such page. You can just buy twitter followers for your page.

    ReplyDelete
  10. I am here to appreciate Dr OGU for using his herbal medicine to cure my Herpes virus. Is about 2 years and 6 months now I have been living with this virus and it has been a serious problem to me, I was so confused cause i have been taking several drugs to be cured but all of my effort was in vain,one morning i was browsing through the internet then i saw several testimonies about Dr. OGU curing people from Herpes virus and immediately i contacted Dr. on his email: drogugusolutionhome@gmail.com , i told him about my troubles and he told me that i must be cured, he gave me some instructions and which i rightly followed. so he prepared a herbal medicine and sent it to me via DHL which i used for 2 weeks and i was cured everything was like a dream to me and my Herpes virus was totally gone, dr .OGU , God bless you and give you more power and ability for more cures.i dont know if there is any one out there suffering for herpes virus or any of these diseases.. DIABETES, CANCER,GENITAL AND SIMPLEX HERPES,LOW SPERMS COUNT,SYPHILIS, HIV/AIDS, FIBRIOD,COPD,MENINGITIES,HEPATITIES B [HBV] DISEASES .etc why don't you contact dr.Ogu today and be free from your diseases because he is very good and honest Doctor. contact him via email; drogugusolutionhome@gmail.com or text/call via: +1 (719) 629 0982

    ReplyDelete
  11. Satya Puri Studio is Bali Construction have worked on historically-inspired homes & villa around the world — including “Lux Villa in Bali; Sukh Sagar Haveli in Jodhpur, Rajasthan; Kahani Paradise in Gokarna. Their work shows inspiration across history and styles, tied together with the goal of enhancing life through good design.

    ReplyDelete
  12. We’re a Web Design Bali with over a decade of experience in building user friendly, secure websites that help strengthen your business.

    ReplyDelete
  13. Whether you’re looking for income, growth or a combination of both, we offer quality advice, comprehensive Investment Management and ongoing service. With regular reviews, we adjust your portfolio to meet your changing needs and current market conditions to help you achieve your financial goals.

    ReplyDelete
  14. Queens Tandoor is the best vegan curry house in Bali. Our vegan menu crafted by Indian restaurant specialists and top-rated chefs, is the first of its kind on Seminyak We feel that everyone in Bali should have the opportunity to experience the difference Queens Indian Cuisine brings to the table, whether you are on Seminyak - Kuta - Nusa Dua - Ubud – the best Indian food in Bali is here at Queens Tandoor. We are Bali's best-kept secret in the search for amazing Indian food.

    ReplyDelete
  15. Looking to incorporate your company in Bali? GRO Indonesia has a notable experience in dealing with company establishment. We have managed to assist a number of companies to assist them in processing their legal documents. We have a range of service options depending on your needs, which allow businesses to have a legal presence in Indonesia.

    ReplyDelete
  16. If you are in search of a great Yoga Course in Bali, Yoga Bali may be the best place to start with. It’s is one of the best yoga teacher training schools in Bali.
    At Yoga Bali we offer unique Yoga Teacher Training in Bali and online and have graduated over 50 successful yoga teacher trainees from all corners of the globe. Our 80 video Yoga Teacher Training Course is internationally accredited. Our 80 Hour Yoga Online Video Training is designed to connect existing teachers with a robust body of further knowledge and professional up-skilling with our dedicated teaching team.

    ReplyDelete
  17. Gerçek bodrum bayan arkadaş ile görüşme gerçekleştirmek için Bodrum bayan arkadaş sayfasına giriş yapabilir, Bodrum bayan arkadaş ilaları inceleyebilirsin: bodrumbebekleri.com

    Bodrum bayan arkadaş ilanlarına göz gezdirmek için hemen bodrum bayan arkadaş sayfasına tıkla birbirinden gerçek bodrum bayan arkadaş profillerini görüntüle: bodrumbebekleri

    Bodrum bayan arkadaş sitesi olan Bodrumbebekleri ile sende hemen bir bodrum bayan arkadaş telefon numarasına ulaşabilirsin. Bodrum bayan arkadaş sayfasına giriş yapmak için tıkla: bodrum

    ReplyDelete
  18. Casino Online - DrmCD
    Join us for exciting 보령 출장마사지 casino games and VIP promos at DrmCD. Casino Online and 정읍 출장마사지 enjoy the benefits 용인 출장마사지 of a generous sign-up bonus of 성남 출장안마 up to $2000. 전주 출장안마

    ReplyDelete
  19. Truly Helpful Post. Thanks for sharing. Don't miss WORLD'S BEST BIKEGAMES


    ReplyDelete
  20. İnstagram takipçi satın al! İnstagram takipçi sitesi ile takipçi satın al sende sosyal medyada fenomen olmaya bir adım at. Sende hemen instagram takipçi satın almak istiyorsan tıkla:

    1- takipçi satın al

    2- takipçi satın al

    3- takipçi satın al

    ReplyDelete