Skip to content

Commit 9439855

Browse files
authored
Merge pull request #2500 from mikeblome/mb-exceptions
Moved exceptions topics from Modern C++ to Exceptions node
2 parents 7ecc4fc + 1deb434 commit 9439855

33 files changed

+157
-180
lines changed

.openpublishing.redirection.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,11 @@
730730
"redirect_url": "/cpp/cpp/cpp-language-reference",
731731
"redirect_document_id": false
732732
},
733+
{
734+
"source_path": "docs/cpp/cpp-exception-handling.md",
735+
"redirect_url": "/cpp/cpp/exception-handling-in-visual-cpp",
736+
"redirect_document_id": false
737+
},
733738
{
734739
"source_path": "docs/cpp/types-of-linkage.md",
735740
"redirect_url": "/cpp/cpp/program-and-linkage-cpp",
@@ -13246,4 +13251,4 @@
1324613251
"redirect_document_id": false
1324713252
}
1324813253
]
13249-
}
13254+
}

docs/build/reference/exceptions-c-cpp.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: "Exceptions (C/C++)"
3-
ms.date: "11/04/2016"
2+
title: "DLL loading exception codes (C/C++)"
3+
ms.date: "11/19/2019"
44
f1_keywords: ["ERROR_MOD_NOT_FOUND", "vcppException", "ERROR_SEVERITY_ERROR"]
55
helpviewer_keywords: ["vcppException", "C++ exception handling, delayed loading of DLLs", "delayed loading of DLLs, exceptions", "ERROR_SEVERITY_ERROR exception", "ERROR_MOD_NOT_FOUND exception"]
66
ms.assetid: c03be05d-1c39-4f35-84cf-00c9af3bae9a

docs/build/reference/gx-enable-exception-handling.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "/GX (Enable Exception Handling)"
3-
ms.date: "11/04/2016"
3+
ms.date: "11/19/2019"
44
f1_keywords: ["/gx"]
55
helpviewer_keywords: ["exception handling, enabling", "/GX compiler option [C++]", "-GX compiler option [C++]", "cl.exe compiler, exception handling", "enable exception handling compiler option [C++]", "GX compiler option [C++]"]
66
ms.assetid: 933b43ba-de77-4ff8-a48b-7074de90bc1c
@@ -21,8 +21,6 @@ Deprecated. Enables synchronous exception handling using the assumption that fun
2121

2222
By default, **/EHsc**, the equivalent of **/GX**, is in effect when you compile by using the Visual Studio development environment. When using the command line tools, no exception handling is specified. This is the equivalent of **/GX-**.
2323

24-
For more information, see [C++ Exception Handling](../../cpp/cpp-exception-handling.md).
25-
2624
### To set this compiler option in the Visual Studio development environment
2725

2826
1. Open the project's **Property Pages** dialog box. For details, see [Set C++ compiler and build properties in Visual Studio](../working-with-project-properties.md).

docs/cpp/bad-cast-exception.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
2-
title: "bad_cast Exception"
2+
title: "bad_cast exception"
33
ms.date: "10/04/2019"
44
f1_keywords: ["bad_cast", "bad_cast_cpp"]
55
helpviewer_keywords: ["exceptions [C++], bad_cast", "bad_cast keyword [C++]"]
66
ms.assetid: 31eae1e7-d8d5-40a0-9fef-64a6a4fc9021
77
---
8-
# bad_cast Exception
8+
# bad_cast exception
99

1010
The **bad_cast** exception is thrown by the **dynamic_cast** operator as the result of a failed cast to a reference type.
1111

@@ -115,4 +115,4 @@ const char* what() const noexcept override;
115115

116116
[dynamic_cast Operator](../cpp/dynamic-cast-operator.md)\
117117
[Keywords](../cpp/keywords-cpp.md)\
118-
[C++ Exception Handling](../cpp/cpp-exception-handling.md)
118+
[Modern C++ best practices for exceptions and error handling](../cpp/errors-and-exception-handling-modern-cpp.md)

docs/cpp/bad-typeid-exception.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
2-
title: "bad_typeid Exception"
2+
title: "bad_typeid exception"
33
ms.date: "10/04/2019"
44
f1_keywords: ["bad_typeid", "bad_typeid_cpp"]
55
helpviewer_keywords: ["bad_typeid exception", "exceptions [C++], bad_typeid"]
66
ms.assetid: 5963ed58-4ede-4597-957d-f7bbd06299c2
77
---
8-
# bad_typeid Exception
8+
# bad_typeid exception
99

1010
The **bad_typeid** exception is thrown by the [typeid operator](../cpp/typeid-operator.md) when the operand for **typeid** is a NULL pointer.
1111

docs/cpp/cleaning-up-resources.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,5 @@ int main() {
6060

6161
## See also
6262

63-
[Writing a Termination Handler](../cpp/writing-a-termination-handler.md)<br/>
63+
[Writing a termination handler](../cpp/writing-a-termination-handler.md)<br/>
6464
[Structured Exception Handling (C/C++)](../cpp/structured-exception-handling-c-cpp.md)

docs/cpp/cpp-exception-handling.md

Lines changed: 0 additions & 37 deletions
This file was deleted.

docs/cpp/errors-and-exception-handling-modern-cpp.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
2-
title: "Errors and Exception Handling (Modern C++)"
3-
ms.date: "05/07/2019"
2+
title: "Modern C++ best practices for exceptions and error handling"
3+
ms.date: "11/19/2019"
44
ms.topic: "conceptual"
55
ms.assetid: a6c111d0-24f9-4bbb-997d-3db4569761b7
66
---
7-
# Errors and Exception Handling (Modern C++)
7+
# Modern C++ best practices for exceptions and error handling
88

99
In modern C++, in most scenarios, the preferred way to report and handle both logic errors and runtime errors is to use exceptions. This is especially true when the stack might contain several function calls between the function that detects the error and the function that has the context to know how to handle it. Exceptions provide a formal, well-defined way for code that detects errors to pass the information up the call stack.
1010

@@ -54,7 +54,7 @@ int main()
5454
}
5555
```
5656
57-
Exceptions in C++ resemble those in languages such as C# and Java. In the **try** block, if an exception is *thrown* it will be *caught* by the first associated **catch** block whose type matches that of the exception. In other words, execution jumps from the **throw** statement to the **catch** statement. If no usable catch block is found, `std::terminate` is invoked and the program exits. In C++, any type may be thrown; however, we recommend that you throw a type that derives directly or indirectly from `std::exception`. In the previous example, the exception type, [invalid_argument](../standard-library/invalid-argument-class.md), is defined in the standard library in the [\<stdexcept>](../standard-library/stdexcept.md) header file. C++ does not provide, and does not require, a **finally** block to make sure that all resources are released if an exception is thrown. The resource acquisition is initialization (RAII) idiom, which uses smart pointers, provides the required functionality for resource cleanup. For more information, see [How to: Design for Exception Safety](../cpp/how-to-design-for-exception-safety.md). For information about the C++ stack-unwinding mechanism, see [Exceptions and Stack Unwinding](../cpp/exceptions-and-stack-unwinding-in-cpp.md).
57+
Exceptions in C++ resemble those in languages such as C# and Java. In the **try** block, if an exception is *thrown* it will be *caught* by the first associated **catch** block whose type matches that of the exception. In other words, execution jumps from the **throw** statement to the **catch** statement. If no usable catch block is found, `std::terminate` is invoked and the program exits. In C++, any type may be thrown; however, we recommend that you throw a type that derives directly or indirectly from `std::exception`. In the previous example, the exception type, [invalid_argument](../standard-library/invalid-argument-class.md), is defined in the standard library in the [\<stdexcept>](../standard-library/stdexcept.md) header file. C++ does not provide, and does not require, a **finally** block to make sure that all resources are released if an exception is thrown. The resource acquisition is initialization (RAII) idiom, which uses smart pointers, provides the required functionality for resource cleanup. For more information, see [How to: Design for Exception Safety](how-to-design-for-exception-safety.md). For information about the C++ stack-unwinding mechanism, see [Exceptions and Stack Unwinding](exceptions-and-stack-unwinding-in-cpp.md).
5858
5959
## Basic guidelines
6060
@@ -64,7 +64,7 @@ Robust error handling is challenging in any programming language. Although excep
6464
6565
- Use exceptions when the code that handles the error might be separated from the code that detects the error by one or more intervening function calls. Consider whether to use error codes instead in performance-critical loops when code that handles the error is tightly-coupled to the code that detects it.
6666
67-
- For every function that might throw or propagate an exception, provide one of the three exception guarantees: the strong guarantee, the basic guarantee, or the nothrow (noexcept) guarantee. For more information, see [How to: Design for Exception Safety](../cpp/how-to-design-for-exception-safety.md).
67+
- For every function that might throw or propagate an exception, provide one of the three exception guarantees: the strong guarantee, the basic guarantee, or the nothrow (noexcept) guarantee. For more information, see [How to: Design for Exception Safety](how-to-design-for-exception-safety.md).
6868
6969
- Throw exceptions by value, catch them by reference. Don’t catch what you can't handle.
7070
@@ -86,15 +86,15 @@ Exceptions and asserts are two distinct mechanisms for detecting run-time errors
8686
8787
Both C and C++ programs can use the structured exception handling (SEH) mechanism in the Windows operating system. The concepts in SEH resemble those in C++ exceptions, except that SEH uses the **__try**, **__except**, and **__finally** constructs instead of **try** and **catch**. In the Microsoft C++ compiler (MSVC), C++ exceptions are implemented for SEH. However, when you write C++ code, use the C++ exception syntax.
8888
89-
For more information about SEH, see [Structured Exception Handling (C/C++)](../cpp/structured-exception-handling-c-cpp.md).
89+
For more information about SEH, see [Structured Exception Handling (C/C++)](structured-exception-handling-c-cpp.md).
9090
9191
## Exception specifications and noexcept
9292
93-
Exception specifications were introduced in C++ as a way to specify the exceptions that a function might throw. However, exception specifications proved problematic in practice, and are deprecated in the C++11 draft standard. We recommend that you do not use exception specifications except for `throw()`, which indicates that the function allows no exceptions to escape. If you must use exception specifications of the type `throw(`*type*`)`, be aware that MSVC departs from the standard in certain ways. For more information, see [Exception Specifications (throw)](../cpp/exception-specifications-throw-cpp.md). The `noexcept` specifier is introduced in C++11 as the preferred alternative to `throw()`.
93+
Exception specifications were introduced in C++ as a way to specify the exceptions that a function might throw. However, exception specifications proved problematic in practice, and are deprecated in the C++11 draft standard. We recommend that you do not use exception specifications except for `throw()`, which indicates that the function allows no exceptions to escape. If you must use exception specifications of the type `throw(`*type*`)`, be aware that MSVC departs from the standard in certain ways. For more information, see [Exception Specifications (throw)](exception-specifications-throw-cpp.md). The `noexcept` specifier is introduced in C++11 as the preferred alternative to `throw()`.
9494
9595
## See also
9696
97-
[How to: Interface Between Exceptional and Non-Exceptional Code](../cpp/how-to-interface-between-exceptional-and-non-exceptional-code.md)<br/>
98-
[Welcome Back to C++ (Modern C++)](../cpp/welcome-back-to-cpp-modern-cpp.md)<br/>
99-
[C++ Language Reference](../cpp/cpp-language-reference.md)<br/>
97+
[How to: Interface Between Exceptional and Non-Exceptional Code](how-to-interface-between-exceptional-and-non-exceptional-code.md)<br/>
98+
[Welcome Back to C++ (Modern C++)](welcome-back-to-cpp-modern-cpp.md)<br/>
99+
[C++ Language Reference](cpp-language-reference.md)<br/>
100100
[C++ Standard Library](../standard-library/cpp-standard-library-reference.md)
Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,55 @@
11
---
2-
title: "Exception Handling in MSVC"
3-
ms.date: "05/07/2019"
2+
title: "Exception handling in MSVC"
3+
ms.date: "11/19/2019"
44
helpviewer_keywords: ["try-catch keyword [C++], exception handling"]
55
ms.assetid: a6aa08de-669d-4ce8-9ec3-ec20d1354fcf
66
---
7-
# Exception Handling in MSVC
7+
# Exception handling in MSVC
88

9-
An exception is an error condition, possibly outside the program's control, that prevents the program from continuing along its regular execution path. Certain operations, including object creation, file input/output, and function calls made from other modules, are all potential sources of exceptions even when your program is running correctly. Robust code anticipates and handles exceptions.
9+
An exception is an error condition, possibly outside the program's control, that prevents the program from continuing along its regular execution path. Certain operations, including object creation, file input/output, and function calls made from other modules, are all potential sources of exceptions even when your program is running correctly. Robust code anticipates and handles exceptions. To detect logic errors, use assertions rather than exceptions (see [Using Assertions](/visualstudio/debugger/c-cpp-assertions)).
1010

11-
To detect logic errors within a single program or module, use assertions rather than exceptions (see [Using Assertions](/visualstudio/debugger/c-cpp-assertions)).
11+
## Kinds of exceptions
1212

1313
The Microsoft C++ compiler (MSVC) supports three kinds of exception handling:
1414

15-
- [C++ exception handling](../cpp/cpp-exception-handling.md)
15+
- [C++ exception handling](errors-and-exception-handling-modern-cpp.md)
1616

1717
For most C++ programs, you should use C++ exception handling, which is type-safe and ensures that object destructors are invoked during stack unwinding.
1818

19-
- [Structured exception handling](../cpp/structured-exception-handling-c-cpp.md)
19+
- [Structured exception handling](structured-exception-handling-c-cpp.md)
2020

2121
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.
2222

2323
- [MFC exceptions](../mfc/exception-handling-in-mfc.md)
2424

25-
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. 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 MSVC 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.
25+
Use the [/EH](../build/reference/eh-exception-handling-model.md) compiler option to specify the type of exception handling to use in a project; C++ exception handling is the default. Do not mix the error handling mechanisms; for example, do not use C++ exceptions with structured exception handling. Using C++ exception handling makes your code more portable, and it allows you to handle exceptions of any type. For more information about the drawbacks of structured exception handling, see [Structured Exception Handling](structured-exception-handling-c-cpp.md). For advice about mixing MFC macros and C++ exceptions, see [Exceptions: Using MFC Macros and C++ Exceptions](../mfc/exceptions-using-mfc-macros-and-cpp-exceptions.md).
2626

27-
Use the [/EH](../build/reference/eh-exception-handling-model.md) compiler option to specify the type of exception handling to use in a project; C++ exception handling is the default. Do not mix the error handling mechanisms; for example, do not use C++ exceptions with structured exception handling. Using C++ exception handling makes your code more portable, and it allows you to handle exceptions of any type. For more information about the drawbacks of structured exception handling, see [Structured Exception Handling](../cpp/structured-exception-handling-c-cpp.md). For advice about mixing MFC macros and C++ exceptions, see [Exceptions: Using MFC Macros and C++ Exceptions](../mfc/exceptions-using-mfc-macros-and-cpp-exceptions.md).
27+
## In this section
2828

29-
For information on handling exceptions in CLR applications, see [Exception Handling (C++/CLI and C++/CX)](../extensions/exception-handling-cpp-component-extensions.md).
29+
- [Modern C++ best practices for exceptions and error handling](errors-and-exception-handling-modern-cpp.md)
3030

31-
For information about exception handling on x64 processors, see [x64 exception handling](../build/exception-handling-x64.md).
31+
- [How to design for exception safety](how-to-design-for-exception-safety.md)
32+
33+
- [How to interface between exceptional and non-exceptional code](how-to-interface-between-exceptional-and-non-exceptional-code.md)
34+
35+
- [The try, catch, and throw Statements](try-throw-and-catch-statements-cpp.md)
36+
37+
- [How Catch Blocks are Evaluated](how-catch-blocks-are-evaluated-cpp.md)
38+
39+
- [Exceptions and Stack Unwinding](exceptions-and-stack-unwinding-in-cpp.md)
40+
41+
- [Exception Specifications](exception-specifications-throw-cpp.md)
42+
43+
- [noexcept](noexcept-cpp.md)
44+
45+
- [Unhandled C++ Exceptions](unhandled-cpp-exceptions.md)
46+
47+
- [Mixing C (Structured) and C++ Exceptions](mixing-c-structured-and-cpp-exceptions.md)
48+
49+
- [Structured Exception Handling (SEH) (C/C++)](structured-exception-handling-c-cpp.md)
3250

3351
## See also
3452

35-
[C++ Language Reference](../cpp/cpp-language-reference.md)
53+
[C++ Language Reference](cpp-language-reference.md)</br>
54+
[x64 exception handling](../build/exception-handling-x64.md)</br>
55+
[Exception Handling (C++/CLI and C++/CX)](../extensions/exception-handling-cpp-component-extensions.md)

docs/cpp/exception-specifications-throw-cpp.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
2-
title: "Exception Specifications (throw, noexcept) (C++)"
2+
title: "Exception specifications (throw, noexcept) (C++)"
33
ms.date: "01/18/2018"
44
helpviewer_keywords: ["exceptions [C++], exception specifications", "throwing exceptions [C++], throw keyword", "C++ exception handling [C++], throwing exceptions", "throw keyword [C++]", "noexcept keyword [C++]"]
55
ms.assetid: 4d3276df-6f31-4c7f-8cab-b9d2d003a629
66
---
7-
# Exception Specifications (throw, noexcept) (C++)
7+
# Exception specifications (throw, noexcept) (C++)
88

99
Exception specifications are a C++ language feature that indicate the programmer's intent about the exception types that can be propagated by a function. You can specify that a function may or may not exit by an exception by using an *exception specification*. The compiler can use this information to optimize calls to the function, and to terminate the program if an unexpected exception escapes the function.
1010

@@ -119,4 +119,4 @@ in handler
119119
## See also
120120

121121
[try, throw, and catch Statements (C++)](../cpp/try-throw-and-catch-statements-cpp.md)<br/>
122-
[C++ Exception Handling](../cpp/cpp-exception-handling.md)
122+
[Modern C++ best practices for exceptions and error handling](errors-and-exception-handling-modern-cpp.md)

0 commit comments

Comments
 (0)