Microsoft-specific exception handling mechanisms#SEH

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).{{cite web|url=http://www.devx.com/microsoftISV/Article/15992 |title=Vectored Exception Handling in Windows Server 2003 (Through Internet Archive) |archive-url = https://web.archive.org/web/20080118162324/http://www.devx.com/microsoftISV/Article/15992 |archive-date = 2008-01-18}} 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 =

{{see also|Exception handling syntax#Microsoft-specific}}

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,{{cite web |url=https://msdn.microsoft.com/en-us/library/ms680659.aspx |title=Structured Exception Handling Functions |date=2009-11-12 |work=MSDN Library |author=Microsoft Corp. |access-date=2022-07-23 }} for example, RaiseException to raise SEH exceptions manually.

= 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 {{mono|_EXCEPTION_REGISTRATION_RECORD}} list at the start of its Thread Information Block. The __try statement essentially calls a compiler-defined EH_prolog function. That function allocates an {{mono|_EXCEPTION_REGISTRATION_RECORD}} on the stack pointing to the __except_handler3{{Efn|The name varies in different versions of VC runtime}} function in msvcrt.dll,{{Efn|ntdll.dll and kernel32.dll, as well as other programs linked statically with VC runtime, have this function compiled-in instead}} 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 {{mono|_EXCEPTION_REGISTRATION_RECORD}} created by EH_prolog is extended with a few additional fields used by __except_handler3.{{cite web|url=http://stoned-vienna.com/html/index.php?page=windows-exception-handling|author=Peter Kleissner|title=Windows Exception Handling - Peter Kleissner|date=February 14, 2009|access-date=2009-11-21 |archive-url=https://web.archive.org/web/20131014204335/http://stoned-vienna.com/html/index.php?page=windows-exception-handling |archive-date=October 14, 2013 |url-status=dead}}, Compiler based Structured Exception Handling section

In the case of an exception in user mode code, the operating system{{Efn|More specifically, ntdll!RtlDispatchException system routine called from ntdll!KiUserExceptionDispatcher which is in turn called from the nt!KiDispatchException kernel function. (See {{cite web|url=http://www.nynaeve.net/?p=201|title=A catalog of NTDLL kernel mode to user mode callbacks, part 2: KiUserExceptionDispatcher|author=Ken Johnson|date=November 16, 2007 }} for details)}} parses the thread's {{mono|_EXCEPTION_REGISTRATION_RECORD}} 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.{{Efn|The message can be silenced by altering the process's [https://msdn.microsoft.com/en-us/library/ms680548.aspx error mode]; the default last handler can be replaced with [https://msdn.microsoft.com/en-us/library/ms680634.aspx SetUnhandledExceptionFilter] API}} Then the list is traversed once more giving handlers a chance to clean up any resources used. Finally, the execution returns to kernel mode{{Efn|ntdll!KiUserExceptionDispatcher calls either nt!ZwContinue or nt!ZwRaiseException}} 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.{{cite web |title=Exceptional Behavior - x64 Structured Exception Handling |url=https://www.osronline.com/article.cfm%5earticle=469.htm |publisher=The NT Insider}}{{cite web |title=x64 exception handling |url=https://docs.microsoft.com/en-us/cpp/build/exception-handling-x64?view=vs-2019 |website=VC++ 2019 documentation | date=8 February 2022 |language=en-us}}

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.{{cite web |title=MSVC compatibility |url=http://clang.llvm.org/docs/MSVCCompatibility.html |website=Clang 11 documentation}}

{{Anchor|VEH}}Vectored Exception Handling

Vectored Exception Handling was introduced in Windows XP.{{cite web|url=https://msdn.microsoft.com/en-us/magazine/cc301714.aspx|title=Under the Hood: New Vectored Exception Handling in Windows XP |archive-url = https://web.archive.org/web/20080915135659/https://msdn.microsoft.com/en-us/magazine/cc301714.aspx |archive-date = 2008-09-15 |url-status=dead}} 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.{{cite web|url=https://msdn.microsoft.com/en-us/magazine/cc300448.aspx|title=Windows Server 2003 Discover Improved System Info, New Kernel, Debugging, Security, and UI APIs |archive-url = https://web.archive.org/web/20080505055123/https://msdn.microsoft.com/en-us/magazine/cc300448.aspx |archive-date = 2008-05-05 |url-status=dead}}

Notes

{{Notelist|30em}}

References

{{Reflist|30em}}