Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I decode a C++ exception from a Windows SEH exception? (And if so, how?)

In the event of an unhandled C++ exception I want to print:

  1. The message (what()) of the C++ exception
  2. A stack trace.

In order to get the stack trace, I'm using SetUnhandledExceptionFilter in combination with the StackWalker library:

struct FooStackWalker : StackWalker
{
    virtual void OnCallstackEntry(CallstackEntryType, CallstackEntry &entry) override
    {
        std::cerr << entry.lineFileName << " (" << entry.lineNumber << "): " << entry.undFullName << std::endl;
    }
};

LONG WINAPI UnhandledExceptionHandler(LPEXCEPTION_POINTERS pointers)
{
    FooStackWalker walker;
    walker.ShowCallstack(::GetCurrentThread(), pointers->ContextRecord);
    ::TerminateProcess(::GetCurrentProcess(), 1);
}


int main()
{
    ::SetUnhandledExceptionFilter(UnhandledExceptionHandler);
}

I've gotten the stack trace to print just fine, but now getting what is difficult.

Is there some way I can decode the SEH exception as a C++ exception in order to call this member function before termination?

like image 493
Billy ONeal Avatar asked Oct 30 '12 21:10

Billy ONeal


People also ask

What is an exception How is an exception handled in C++?

Exception handling in C++ consist of three keywords: try , throw and catch : The try statement allows you to define a block of code to be tested for errors while it is being executed. The throw keyword throws an exception when a problem is detected, which lets us create a custom error.

What is SEH error?

Structured exception handling (SEH) is an exception handling mechanism included in most programs to make them robust and reliable. It is used to handle many types of errors and any exceptions that arise during the normal execution of an application.

What is structured exception handling seh?

Structured exception handling (SEH) is a Microsoft extension to C and C++ to handle certain exceptional code situations, such as hardware faults, gracefully. Although Windows and Microsoft C++ support SEH, we recommend that you use ISO-standard C++ exception handling in C++ code.

What are the types of exceptions in C++?

There are two types of exceptions: a)Synchronous, b)Asynchronous (i.e., exceptions which are beyond the program's control, such as disc failure, keyboard interrupts etc.). C++ provides the following specialized keywords for this purpose: try: Represents a block of code that can throw an exception.


1 Answers

Why not use the C++ machinery that already gives you the exception details? It's not exclusive with SEH filters (although it is exclusive with SetUnhandledExceptionFilter). You just have to nest the handlers correctly:

int main()
{
    try {
        return cppexcept_main();
    }
    catch (const std::exception& e)
    {
        //use e.what()
    }
}

int cppexcept_main()
{
    __try {
        return application_main();
    }
    __except(GrabStackTrace(GetExceptionInformation()), EXCEPTION_CONTINUE_SEARCH) {
         /* never reached due to EXCEPTION_CONTINUE_SEARCH */
    }
}
like image 175
Ben Voigt Avatar answered Oct 06 '22 00:10

Ben Voigt