Microsoft-specific exception handling mechanisms
Microsoft Windows OS family employs some exception handling mechanisms that are based on the operating system specifics.
Contents
Structured Exception Handling
Microsoft Structured Exception Handling is the native exception handling mechanism for Windows and a forerunner technology to Vectored Exception Handling (VEH).[1] It features the finally
mechanism not present in standard С++ exceptions (but present in most imperative languages introduced later). SEH is set up and handled separately for each thread of execution.
The Microsoft implementation of SEH is based on a patent licensed from Borland, U.S. Patent 5,628,016. Open-source operating systems have resisted adopting a SEH-based mechanism due to this patent.[2]
Usage
<templatestyles src="Module:Hatnote/styles.css"></templatestyles>
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,[3] for example, RaiseException
to raise SEH exceptions manually.
Implementation
Each thread of execution in Windows has a link to an undocumented _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 _EXCEPTION_REGISTRATION_RECORD on the stack pointing to __except_handler3
[lower-alpha 1] function in msvcrt.dll
,[lower-alpha 2] 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 such blocks are present, _EXCEPTION_REGISTRATION_RECORD being created is extended with a few additional fields used by __except_handler3
.[4]
In a case of an exception in a user mode code, the operating system[lower-alpha 3] parses the thread's _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.[lower-alpha 4] Then the list is traversed once more giving handlers a chance to clean up any resources used. Finally, the execution returns to kernel mode[lower-alpha 5] where the process is either resumed or terminated.
Vectored Exception Handling
Vectored Exception Handling was introduced in Windows XP.[5] 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.[1][5] Compared with SEH, VEH works more like a traditional notification callback scheme.[6]
Notes
<templatestyles src="Reflist/styles.css" />
Cite error: Invalid <references>
tag; parameter "group" is allowed only.
<references />
, or <references group="..." />
References
<templatestyles src="Reflist/styles.css" />
Cite error: Invalid <references>
tag; parameter "group" is allowed only.
<references />
, or <references group="..." />
External links
- Lua error in package.lua at line 80: module 'strict' not found.
- Lua error in package.lua at line 80: module 'strict' not found. Note that the examples given there do not work as-is on modern Windows systems (post XP SP2) due to the changes Microsoft made to address the security issues present in the early SEH design. The examples still work on later versions of Windows if compiled with
/link /safeseh:no
. - Lua error in package.lua at line 80: module 'strict' not found.
- US patent 7,480,919 - Safe exceptions
- Lua error in package.lua at line 80: module 'strict' not found. Covers the obscure details needed to get low-level SEH (and particularly SafeSEH) code to work on more modern Windows.
- Lua error in package.lua at line 80: module 'strict' not found.
- Lua error in package.lua at line 80: module 'strict' not found.
- Lua error in package.lua at line 80: module 'strict' not found.
- Lua error in package.lua at line 80: module 'strict' not found. An article explaining why Windows 7 SP1 ignores SafeSEH for some older binaries, while Windows XP SP3 honors it.
- ↑ 1.0 1.1 Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found., Compiler based Structured Exception Handling section
- ↑ 5.0 5.1 Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
Cite error: <ref>
tags exist for a group named "lower-alpha", but no corresponding <references group="lower-alpha"/>
tag was found, or a closing </ref>
is missing