Sunday, May 8, 2016

You can try or you can __try

Some time ago, I've outlined a technique for wrapping a C++ fragment in a Structured Exception Handling (SEH) crash catcher. It involved a rather convoluted sequence of C++ to C back to C++ calls.

I'm glad to report that this is no longer necessary. As of Visual Studio 2015 Update 2, one can freely mix and match try/catch with __try/__except in a C++ source. So the SafeCall template I've once presented simplifies to something like this:

template<typename TFunctor>
void SafeCall(const TFunctor &f)
{
    CONTEXT Ctxt;
    void *Address = 0;
    __try
    {
        f();
    }
    __except (ExcFilter(GetExceptionCode(), GetExceptionInformation(), Ctxt, Address))
    {
        OnCrash(GetExceptionCode(), Address, &Ctxt);
    }
}