0

I am writing code to test C++ and SEH exceptions. However, on running my code below, the exception again calls the same handler even though I have specified the index to be 0 i.e. the last handler. In my opinion, the ideal handling should be:

SEH exception happens -> Vector Exception Handler is called -> Searches and sees no other handler -> program terminates/Exception is left unhandled.

On running the debugger, I see the output as:

exception caught!

(multiple times when I click on "continue" on VS)

LONG WINAPI HandleVecException(EXCEPTION_POINTERS *exceptionPointers) {
    std::cout << "Exception caught!";
    return EXCEPTION_CONTINUE_SEARCH;
}

int main() {
    // Install the exception filter for the main thread
    PVOID handle = AddVectoredExceptionHandler(0,HandleVecException);
    try {       
        int* ptr = nullptr;
        *ptr = 42; // Access violation
    }
    catch(std::exception ex){
        //code won't enter here
        cout << "Exception" << ex.what();
        //std::rethrow_exception(current_exception());
    }

    RemoveVectoredExceptionHandler(handle);

    return 0;
}
Batmon
  • 21
  • 5
  • 2
    Read the [documentation](https://learn.microsoft.com/en-us/windows/win32/debug/vectored-exception-handling). The index you specify is only in relation to other VEH handlers. VEH handling does not *replace* SEH handling, only extends it. All VEH handlers are called first, and a VEH handler can only continue the search or jump back to the original execution point. If all VEH handlers continue searching, then SEH handlers are called next. And FYI, an Access Violation will not raise an `std::exception` object that you can `catch`, unless you have an SEH handler that performs that translation. – Remy Lebeau Jun 03 '23 at 07:40
  • I never meant to say std::exception will catch it, that is unrelated to my question. You said once all VEH handlers continue searching, then SEH handlers are called next. That doesn't happen for me and the vector handler is again called in my case. – Batmon Jun 03 '23 at 19:12

0 Answers0