c++ - 在什么情况下EXCEPTION_RECORD链接到另一个嵌套异常?

标签 c++ exception structured-exception nested-exceptions

_EXCEPTION_RECORDdocumentation说明了其中一个成员struct _EXCEPTION_RECORD *ExceptionRecord

A pointer to an associated EXCEPTION_RECORD structure. Exception records can be chained together to provide additional information when nested exceptions occur.



但是,我无法引发这种嵌套结构异常的情况。到目前为止,这是我尝试过的:
#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的链接?

最佳答案

根据MSDN:

When an exception is raised during the processing of an exception filter within ... native code ... a nested exception is raised, the ExceptionRecord field in the EXCEPTION_RECORD structure (as returned by GetExceptionInformation) is set, and the ExceptionFlags field sets the 0x10 bit. The following example illustrates this difference in behavior:

#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将指向原始异常。

关于c++ - 在什么情况下EXCEPTION_RECORD链接到另一个嵌套异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51764125/

相关文章:

c++ - LUNA 中的 Eclipse 发现选项

.net - 为什么.NET的StringValidator的Validate方法在不成功时会抛出异常?

winapi - UnhandledExceptionFilter 的 "application-defined"到底是什么?

c++ - 将外部错误代码映射到 std::error_condition

exception - 如何使用 MYSQLI_REPORT_STRICT 使 mysqli 抛出异常?

windows - 非托管 Windows 进程崩溃的方式?

尝试从 Web 显示 JSON 文件时,C++ Rest 给我一个错误

c++ - 当我从内联函数返回字符串时,会创建多少个临时对象?

c++ - 插入新项目后如何获取布局的高度?

Maven 故障安全插件 - SurefireBooterForkException : There was an error in the forked process (TypeNotPresentExceptionProxy)