2

文档_EXCEPTION_RECORD说它的一个成员,struct _EXCEPTION_RECORD *ExceptionRecord

指向关联的 EXCEPTION_RECORD 结构的指针。异常记录可以链接在一起,以便在发生嵌套异常时提供附加信息。

但是,我还没有能够引发嵌套结构化异常的这种情况。这是我到目前为止所尝试的:

#include <iostream>
#include <windows.h>

void Handle0(LPEXCEPTION_POINTERS pex) {
    std::cout << "chain0 = " << pex->ExceptionRecord->ExceptionRecord << std::endl;
}

void Handle1(LPEXCEPTION_POINTERS pex) {
    std::cout << "chain1 = " << pex->ExceptionRecord->ExceptionRecord << std::endl;
    __try {
        throw 3;
    } __except( Handle0(GetExceptionInformation()), EXCEPTION_EXECUTE_HANDLER ) {}
}

int main() {
    __try {
        throw 3;
    } __except( Handle1(GetExceptionInformation()), EXCEPTION_EXECUTE_HANDLER ) {}
    return 0;
}

pex->ExceptionRecord->ExceptionRecord总是nullptr。_ 在什么情况下我会得到一个嵌套_EXCEPTION_RECORD在那里的链接?

4

1 回答 1

0

根据MSDN

当在处理 ... 本机代码 ... 中的异常过滤器期间引发异常时,引发嵌套异常, 结构ExceptionRecord中的字段EXCEPTION_RECORD(由 返回GetExceptionInformation)被设置,并且该ExceptionFlags字段设置0x10位。以下示例说明了这种行为差异:

#include <windows.h>
#include <stdio.h>
#include <assert.h>

#ifndef false
#define false 0
#endif

int *p;

int filter(PEXCEPTION_POINTERS ExceptionPointers) {
   PEXCEPTION_RECORD ExceptionRecord =
                     ExceptionPointers->ExceptionRecord;

   if ((ExceptionRecord->ExceptionFlags & 0x10) == 0) {
      // not a nested exception, throw one
      *p = 0; // throw another AV
   }
   else {
      printf("Caught a nested exception\n");
      return 1;
    }

   assert(false);

   return 0;
}

void f(void) {
   __try {
      *p = 0;   // throw an AV
   }
   __except(filter(GetExceptionInformation())) {
      printf_s("We should execute this handler if "
                 "compiled to native\n");
    }
}

int main() {
   __try {
      f();
   }
   __except(1) {
      printf_s("The handler in main caught the "
               "exception\n");
    }
}

我相信如果您尝试继续不可持续的异常,它也会被设置。在这种情况下EXCEPTION_RECORD将表示EXCEPTION_NONCONTINUABLE_EXCEPTION,而ExceptionRecord它将指向原始异常。

于 2020-07-09T10:53:54.140 回答