[英]Under what circumstances does EXCEPTION_RECORD link to another nested exception?
_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
的鏈接?
根據MSDN :
當在處理異常過濾器期間引發異常時......本機代碼......引發嵌套異常,
EXCEPTION_RECORD
結構中的ExceptionRecord
字段(由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
將指向原始異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.