다음을 통해 공유


C++ Exception Handling

Exceptions are run-time anomalies, such as division by zero, that require immediate handling when encountered by your program. The C++ language provides built-in support for raising and handling exceptions. With C++ exception handling, your program can communicate unexpected events to a higher execution context that is better able to recover from such abnormal events. These exceptions are handled by code that is outside the normal flow of control.

This discussion on C++ exception handling includes:

Unlike the Win32 structured exception handling mechanism, the language itself provides support for C++ exception handling. These articles describe the Microsoft implementation of C++ exception handling, which is based on the ANSI C++ Standard.

For C++ programs, you should use C++ exception handling rather than structured exception handling. While structured exception handling works in C++ programs, you can ensure that your code is more portable by using C++ exception handling. The C++ exception handling mechanism is more flexible, in that it can handle exceptions of any type. C exceptions are always of type unsigned int.

Using C++ Exceptions

In C++, the process of raising an exception is called "throwing" an exception. A designated exception handler then "catches" the thrown exception.

C++ exception handling is thread safe. Throwing on one thread (including rethrow) has no impact on any throw/catch in progress on another thread.

To enable C++ exception handling in your code, use /EHsc.

참고

As of version 4.0, MFC uses the C++ exception handling mechanism. Although you are encouraged to use C++ exception handling in new code, MFC version 4.0 and later retains the macros from previous versions of MFC so that old code will not be broken. The macros and the new mechanism can be combined as well. For information on mixing macros and C++ exception handling and on converting old code to use the new mechanism, see the articles Exceptions: Using MFC Macros and C++ Exceptions and Exceptions: Converting from MFC Exception Macros.

See Also

Reference

Exception Handling in Visual C++