OS provides SEH to deal directly with crashes.

SEH is language independent but is usually implemented in C/C++.

Pair __try/__except allows to set code inside __try and handle exception in __except (http://msdn.microsoft.com/de-de/library/s58ftw19.aspx). Pair __try/__finally ensures that section of code in __finally is always executed even if __try block terminates prematurely (http://msdn.microsoft.com/de-de/library/9xtt5hxz.aspx).

__except is an exception filter for one of 3 values:

Value Meaning
EXCEPTION_EXECUTE_HANDLER The system transfers control to the exception handler, and execution continues in the stack frame in which the handler is found.
EXCEPTION_CONTINUE_SEARCH The system continues to search for a handler.
EXCEPTION_CONTINUE_EXECUTION The system stops its search for a handler and returns control to the point at which the exception occurred. If the exception is noncontinuable, this results in an EXCEPTION_NONCONTINUABLE_EXCEPTION exception.
void f() { __try { __try { // some code } __except (EXCEPTION_EXECUTE_HANDLER) { // process the exception } } __finally { // executed regardless of whether function caused a crash } }

Exception handler can determine exception value with GetExceptionCode function (http://msdn.microsoft.com/en-us/library/ms679356(VS.85).aspx) which can be called only in exception filters.

__try { n = x / y; } __except (EXCEPTION_INT_DIVIDE_BY_ZERO == GetExceptionCode() ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) { n = NaN; }

In __except user function can be called. GetExceptionInformation (http://msdn.microsoft.com/en-us/library/ms679357(VS.85).aspx) returns a pointer to structure with exception info.

To generate an exception RaiseException (http://msdn.microsoft.com/en-us/library/ms680552(VS.85).aspx) can be used.

SEH limitations:

  • error codes are limited to unsigned int.
  • doesn’t mix with C++ exceptions.
  • doesn’t call any of object destructors.

Additional reading: