This article describes the win32 exception layering and discusses structured exceptions handling (SEH) on OS level and on C++ compiler level.
Describing exceptions from OS level point of view helps to understand functionality and the performance cost of exceptions. Then, it will be not difficult to reveal how it is possible to mix SEH and C++ exceptions and how it is possible to catch SEH in C++ exceptions which could be the main motivation why to read this article.
The best way how to understand the topic is to play with attached examples.
Win32 Exception Layering
The following diagram shows how the common exception service provided by OS (OS Level SEH) is used by C++ compilers for structured exceptions handling (C++ Compiler Level SEH) and for well-known C++ exceptions (C++ Exception Handling).
It is important to understand that both C++ Compiler Level SEH and C++ Exception Handling use the same OS Level SEH.
OS Level SEH
The common exception service provided by OS handles:
Software (Synchronous) Exceptions - explicitly passes control to the operating system through software interrupt
Hardware (Asynchronous) Exceptions - e.g. access violation, integer division by 0, illegal instruction, etc...
Exception Callback Function
Whenever some exception occurs, OS calls the Exception Callback Function within the current thread context.
This function can be defined by the user and the prototype is as follows (defined in excpt.h header):
EXCEPTION_DISPOSITION __cdecl _except_handler
(
struct _EXCEPTION_RECORD* _ExceptionRecord,);
void* _EstablisherFrame,
struct _CONTEXT* _ContextRecord,
void* _DispatcherContext
typedef enum _EXCEPTION_DISPOSITION
{
ExceptionContinueExecution, // tells OS to restart faulting instructionExceptionContinueSearch, // tells OS to continue Exception// Callback Function searchingExceptionNestedException,ExceptionCollidedUnwind
typedef struct _EXCEPTION_RECORD
{
DWORD ExceptionCode; // which exception occurredDWORD ExceptionFlags; // additional exception infostruct _EXCEPTION_RECORD *ExceptionRecord;PVOID ExceptionAddress; // where the exception occurredDWORD NumberParameters;ULONG_PTR ExceptionInformation[EXCEPTION_MAXIMUM_PARAMETERS];
Read more: Codeproject