Share via


Exception Handling: Overview

How Do IFAQDetails

Exceptions occur when a program executes abnormally due to conditions outside the program's control, such as low memory or I/O errors. Abnormal situations should be handled by throwing and catching exceptions.

Abnormal situations are not the same as normal error conditions, such as a function executing correctly but returning a result code indicating an error. A normal error condition, for example, would be a file status function indicating a file doesn't exist. For normal error conditions, examine the error code and respond appropriately.

Abnormal situations are also not the same as erroneous execution, in which, for example, the caller makes some mistake in passing arguments to a function or calls it in an inappropriate context. For erroneous execution, test your inputs and other assumptions with an assertion (see The ASSERT Macro).

Visual C++ supports three kinds of exception handling:

  • C++ exceptions

  • Structured exception handling (SEH), used in C programs for Windows NT and Windows 95

  • MFC exceptions

Note   Since version 3.0, MFC has used C++ exceptions but still supports its older exception handling macros, which are similar to C++ exceptions in form.

Usage Advice

In MFC programs, use C++ exceptions; don't use SEH. Use SEH only in non-MFC programs. Don't mix the two approaches. For advice on mixing MFC macros and C++ exceptions, see Exceptions: Using MFC Macros and C++ Exceptions.

What do you want to know more about?