how to catch array out of bound access error

0

Program terminated without any error and not go into to execute code in except section.

Access the NULL pointer works fine. But access array does not work.

// exceptions_try_except_Statement.cpp
// Example of try-except and try-finally statements
#include <stdio.h>
#include <windows.h> // for EXCEPTION_ACCESS_VIOLATION
#include <excpt.h>

int filter(unsigned int code, struct _EXCEPTION_POINTERS *ep)
{
    printf("in filter.\n");
    if (code == EXCEPTION_ACCESS_VIOLATION)
    {
        printf("caught AV as expected.\n");
        return EXCEPTION_EXECUTE_HANDLER;
    }
    else
    {
        printf("didn't catch AV, unexpected.\n");
        return EXCEPTION_CONTINUE_SEARCH;
    };
}

int main()
{
    int* p = 0x00000000;   // pointer to NULL
    printf("hello\n");
    __try
    {
        printf("in try 1\n");
        __try
        {
            printf("in try 2\n");
            //*p = 13;    // causes an access violation exception;
            int v[1000];
            for(int i=0;i<0xffff;i++)
                v[1000+i]=i;
        }
        __finally
        {
            printf("in finally. termination: \n");
            printf(AbnormalTermination() ? "\tabnormal" : "\tnormal");
            printf("\n");
        }
    }
    __except(filter(GetExceptionCode(), GetExceptionInformation()))
    {
        printf("in except\n");
    }
    printf("world\n");
}

it just output

hello
in try 1
in try 2

I expect it can go into filter function.

How can I do let program execute the filter function before process exit ?

windows
seh
asked on Stack Overflow Apr 24, 2019 by tauruswang

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0