In the event of an unhandled C++ exception I want to print:
what()
) of the C++ exceptionIn 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?
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.
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.
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.
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.
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 */
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With