0

In order to handle access violation exception, project should be configured with SEH Exceptions (/EHa).

I'm looking for handling access violation exceptions only. According to this Microsoft publication here, in order to catch this SEH exceptions, one must use the general catch catch(...) that will catch all exception.

The question is whether there's an option to handle this exception in specific and leave other SEH unhandled, not necessarily by using the try/catch pattern.

UPDATE: thanks to the link in a comment below, I've found out about the __try/__except structure to handle SEH exceptions with many intrinsic functions that may put some more details about the exception nature.

In may case, i only care about AV exception, and i wish to avoid it. However, I'm using C++ and prefer using the standard try/catch structure (while configuring The /EHa compiler option).

Is there an option to use those intrinsic function in order to check the exception type in my case ?

Zohar81
  • 4,554
  • 5
  • 29
  • 82
  • 1
    Use an [exception filter](https://learn.microsoft.com/en-us/cpp/cpp/try-except-statement?view=vs-2017) – Mike Vine Mar 20 '19 at 16:20
  • Although you cannot actually 'handle' AccessViolations. Once an AV has happenned all bets are realistically off with respoect to the state of your program. Remeber an AV isn't _guaranteed_ on bad access as far as c++ is concerned. – Mike Vine Mar 20 '19 at 16:22
  • Hi and thanks for the help, i've found out that there are many intrinsic function that may put some more details about the exception nature. In may case, i only care about AV exception, and i wish to avoid it. However, I'm using C++ and prefer using the standard try-catch structure (while configuring The /EHa compiler option. is there an option to use those intrinsic function in order to check the exception type in my case ? thanks ! – Zohar81 Mar 25 '19 at 07:51

1 Answers1

0

using _set_se_translator I can define the required behavior in SEH exception only, leaving all non-seh exception to the catch statement.

I defined the following handling (should be set per thread) :

void mySEHtranslator(unsigned int exceptionCode, PEXCEPTION_POINTERS exceptionRecord)
{
    if (exceptionRecord->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
    {
        puts("access violation not count");
    }
    else
    {
        throw "1"; // does not suppose to be caught. 
    }
}

and in the relevant code section where i want to imply the translator that ignore access violation :

void mycode()
{
    _set_seh_translator(mySEHtranslator);

    try {
        .... my code ...
    } 
    catch (...) // does not suppose to catch the "1" 
    {

    }
}

additional reference can be found here

Zohar81
  • 4,554
  • 5
  • 29
  • 82