Handling Structured Exceptions (C++)

From RAD Studio
Jump to: navigation, search

Go Up to Structured Exceptions Under Win32 (C++)

Structured exceptions can be handled using an extension of C++ exception handling:

try {
    foo();
}
__except (__expr__) {
    // handler here
}

__expr__ is an expression that evaluates to one of three values:

Value Description
EXCEPTION_CONTINUE_SEARCH (0) The handler is not entered, and the OS continues searching for an exception handler.
EXCEPTION_CONTINUE_EXECUTION (-1) Continue execution at the point of the exception.
EXCEPTION_EXECUTE_HANDLER (1) Enter the exception handler. If the code was compiled with destructor cleanup enabled (-xd, on by default), all destructors for local objects created between the point of the exception and the exception handler are called when the stack is unwound. Stack unwinding is completed before entering the handler.
#include <Windows.h>
#include <excpt.h>

int filter_func(EXCEPTION_POINTERS *);

// ...

EXCEPTION_POINTERS * xp = 0;
try {
    foo();
}
__except (filter_func(xp = GetExceptionInformation())) {
    // ...
}

Or, if you prefer using the comma operator to assignments nested in function calls, see the following example:

__except ((xp = GetExceptionInformation()), filter_func(xp))

See Also