gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:59:03 24 4
gpt4 key购买 nike

_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 exceptionfilter within ... native code ... a nested exception is raised, theExceptionRecord field in the EXCEPTION_RECORD structure (as returnedby GetExceptionInformation) is set, and the ExceptionFlags field setsthe 0x10 bit. The following example illustrates this difference inbehavior:

#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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com