Catching exceptions thrown without try / catch

I remember reading about how exceptions can be caught without using try / catch. Basically, when an exception is thrown, for example, “Unhandled exception”, by defragmenting the null pointer, a process occurs that starts even when the attempt / catch is not coded for the exception. I believe this has something to do with the top-level library that you write and then include in your code. Unfortunately, the documentation for such a method seems to be non-existent, but I have seen / heard about such a method that is executed earlier. Can someone explain how this is done?

+3
source share
3 answers

In C ++, dereferencing a null pointer causes undefined behavior, which does not necessarily mean that an exception is thrown. On Unix systems, for example, a SIGSEGV signal occurs instead.

On Windows, access violations raise the SEH exception . SEH exceptions do not match C ++ exceptions; they are processed using __try/__except(unlike operators try/catch). An unhandled SEH exception throws an unhandled exception filter, which you can set with SetUnhandledExceptionFilter.

#include <Windows.h>
#include <iostream>

LONG WINAPI MyFilter(EXCEPTION_POINTERS * /*ExceptionInfo*/)
{
    std::cout << "An uncaught exception was detected!\n";

    // Ultimately causes Windows Error Reporting to be invoked.
    // Use EXCEPTION_EXECUTE_HANDLER to silently terminate the application.
    return EXCEPTION_CONTINUE_SEARCH;
}

int main()
{
    SetUnhandledExceptionFilter(MyFilter);
    *(char volatile *)0 = 0;
}
+7
source

You might be thinking about the Windows engine. Structured exception mechanism . With full support for C / try / catch semantics, I personally have not found a reasonable use case for this.

+2
source

The fact that operating system granularity, dereferencing a null pointer does not throw a C ++ exception. On Windows, which throws a SEH exception, the MSVC compiler allows them to be processed with non-standard keywords __try and __except. On * nix, which generates a signal, you use the functions in the signal.h header file.

+1
source

All Articles