From Surf Wiki (app.surf) — the open knowledge base
Microsoft-specific exception handling mechanisms
The Microsoft Windows family of operating systems employ some specific exception handling mechanisms.
{{Anchor|SEH}}Structured Exception Handling
Microsoft Structured Exception Handling is the native exception handling mechanism for Windows and a forerunner technology to Vectored Exception Handling (VEH). It features the finally mechanism not present in standard C++ exceptions (but present in most imperative languages introduced later). SEH is set up and handled separately for each thread of execution.
Usage
Microsoft supports SEH as a programming technique at the compiler level only. MS Visual C++ compiler features three non-standard keywords: __try, __except and __finally — for this purpose. Other exception handling aspects are backed by a number of Win32 API functions, for example, RaiseException to raise SEH exceptions manually.
int filterExpression(EXCEPTION_POINTERS* ep) {
ep->ContextRecord->Eip += 8; // divide instruction may be encoded from 2 to 8 bytes
return EXCEPTION_CONTINUE_EXECUTION;
}
int main(void) {
static int zero = 0;
__try {
zero = 1 / zero;
asm {
nop
nop
nop
nop
nop
nop
nop
}
printf("Past the exception.\n");
} __except (filterExpression(GetExceptionInformation())) {
printf("Handler called.\n");
}
return 0;
}
Implementation
IA-32
Each thread of execution in Windows IA-32 edition or the WoW64 emulation layer for the x86-64 version has a link to an undocumented list at the start of its Thread Information Block. The __try statement essentially calls a compiler-defined EH_prolog function. That function allocates an on the stack pointing to the __except_handler3 function in msvcrt.dll, then adds the record to the list's head. At the end of the __try block a compiler-defined EH_epilog function is called that does the reverse operation. Either of these compiler-defined routines can be inline. All the programmer-defined __except and __finally blocks are called from within __except_handler3. If the programmer-defined blocks are present, the created by EH_prolog is extended with a few additional fields used by __except_handler3.
In the case of an exception in user mode code, the operating system parses the thread's list and calls each exception handler in sequence until a handler signals it has handled the exception (by return value) or the list is exhausted. The last one in the list is always the kernel32!UnhandledExceptionFilter which displays the General protection fault error message. Then the list is traversed once more giving handlers a chance to clean up any resources used. Finally, the execution returns to kernel mode where the process is either resumed or terminated.
The patent on this mode of SEH, US5628016, expired in 2014.
x86-64
SEH on 64-bit Windows does not involve a runtime exception handler list; instead, it uses a stack unwinding table (UNWIND_INFO) interpreted by the system when an exception occurs.
This means that the compiler does not have to generate extra code to manually perform stack unwinding and to call exception handlers appropriately. It merely has to emit information in the form of unwinding tables about the stack frame layout and specified exception handlers.
Support
GCC 4.8+ from Mingw-w64 supports using 64-bit SEH for C++ exceptions. LLVM clang supports __try on both x86 and x64.
{{Anchor|VEH}}Vectored Exception Handling
Vectored Exception Handling was introduced in Windows XP. Vectored Exception Handling is made available to Windows programmers using languages such as C++ and Visual Basic. VEH does not replace Structured Exception Handling (SEH); rather, VEH and SEH coexist, with VEH handlers having priority over SEH handlers. Compared with SEH, VEH works more like kernel-delivered Unix signals.
Notes
References
References
- "Vectored Exception Handling in Windows Server 2003 (Through Internet Archive)".
- Microsoft Corp.. (2009-11-12). "Structured Exception Handling Functions". MSDN Library.
- Peter Kleissner. (February 14, 2009). "Windows Exception Handling - Peter Kleissner".
- "Exceptional Behavior - x64 Structured Exception Handling". The NT Insider.
- (8 February 2022). "x64 exception handling".
- "MSVC compatibility".
- "Under the Hood: New Vectored Exception Handling in Windows XP".
- "Windows Server 2003 Discover Improved System Info, New Kernel, Debugging, Security, and UI APIs".
This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page.
Ask Mako anything about Microsoft-specific exception handling mechanisms — get instant answers, deeper analysis, and related topics.
Research with MakoFree with your Surf account
Create a free account to save articles, ask Mako questions, and organize your research.
Sign up freeThis content may have been generated or modified by AI. CloudSurf Software LLC is not responsible for the accuracy, completeness, or reliability of AI-generated content. Always verify important information from primary sources.
Report