如何捕获数组出界访问错误

时间:2019-04-24 00:02:05

标签: windows seh

程序终止,没有任何错误,并且不执行除section中的代码。

访问NULL指针可以正常工作。 但是访问数组不起作用。

// 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");
}

它只是输出

hello
in try 1
in try 2

我希望它可以进入过滤器功能。

如何让程序在进程退出之前执行过滤器功能?

0 个答案:

没有答案