Export (0) Print
Expand All

Exception Handling

Visual Studio .NET 2003

Anticipating and handling exceptions is a hallmark of solid code. Exceptions occur when a program executes abnormally because of conditions outside the program's control. Certain operations, including object creation and file input/output, are subject to failures that go beyond errors. Out-of-memory conditions, for instance, can occur even when your program is running correctly.

Abnormal situations should be handled by throwing and catching exceptions. Such 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 that a file doesn't exist. For normal error conditions, the program should examine the error code and respond appropriately.

Abnormal situations are also not the same as erroneous execution, in which, for example, the caller makes a 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 Using Assertions).

Visual C++ supports three kinds of exception handling:

  • C++ exception handling

    In MFC programs, use C++ exception handling; don't use SEH.

  • Structured exception handling

    Windows supplies its own exception mechanism, called SEH. It is not recommended for C++ or MFC programming. Use SEH only in non-MFC C programs.

  • MFC exceptions

    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. (The older MFC exception handling macros have been supported since version 1.0. Although these macros are not recommended for new programming, they are still supported for backward compatibility. In programs that already use the macros, you can freely use C++ exceptions as well. During preprocessing, the macros evaluate to the exception handling keywords defined in the Visual C++ implementation of the C++ language as of Visual C++ version 2.0. You can leave existing exception macros in place while you begin to use C++ exceptions.)

Do not mix the error handling mechanisms; for example, do not use C++ exceptions with SEH. For advice on mixing MFC macros and C++ exceptions, see Exceptions: Using MFC Macros and C++ Exceptions.

In This Section

C++ Exception Handling
Discusses the exception mechanism that is part of the C++ language (non-MFC).
Exception Handling (SEH)
Describes structured exception handling, which involves cooperation of the operating system but also has direct support in the programming language.

Related Sections

Exception Handling (MFC)
Explains the exception-handling mechanisms available in MFC.
The try, catch, and throw Statements
Describes the C++ exception handling syntax.
C++ Exception Examples
Provides example code that demonstrates the use of exception handlers.
Catching and Deleting Exceptions
Discusses how to catch and delete exceptions in Visual C++.
Exception Handling Overhead
Describes the extra overhead associated with the C++ exception handling mechanism.
Adding Functionality
Provides links to topics describing conceptual information about the Visual C++ libraries and topics discussing various coding technologies and techniques.
© 2014 Microsoft