Share via


Exception Handling Topics (SEH)

OverviewHow Do IFAQ

Windows 95 and Windows NT support a robust approach to handling exceptions, called structured exception handling, which involves cooperation of the operating system but also has direct support in the programming language.

An “exception” is an event that is unexpected or disrupts the ability of the process to proceed normally. Exceptions can be detected by both hardware and software. Hardware exceptions include dividing by zero and overflow of a numeric type. Software exceptions include those you detect and signal to the system by calling the RaiseException function, and special situations detected by Windows 95 and Windows NT.

You can write more reliable code with structured exception handling. You can ensure that resources, such as memory blocks and files, are properly closed in the event of unexpected termination. You can also handle specific problems, such as insufficient memory, with concise, structured code that doesn’t rely on goto statements or elaborate testing of return codes.

Note   These articles describe structured exception handling for the C programming language. Although structured exception handling can also be used with C++, the new C++ exception handling method should be used for C++ programs. See Using Structured Exception Handling with C++ for information on special considerations.

How Structured Exception Handling Works

The traditional approach to exception handling involves passing error codes: one function detects an error and passes an error code to its caller. This process may continue through many levels, until the error is communicated to the function that can properly respond to the error. If there is a weak link in the chain of function calls, the whole procedure fails.

Structured exception handling avoids this propagation of error codes. Its distinctive feature is that after an exception handler is installed, it can handle the exception no matter how many other functions are called. Thus, function A can handle an exception raised inside a function called by A.

What do you want to know more about?