Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/exceptions"

From cppreference.com
< cpp‎ | language
 
m (+mention specifications and terminate/unexpected)
Line 6: Line 6:
  
 
In order for exception to be caught, the throw-expression has to be inside a {{rlp|try_catch|try-block}} or inside a function called from a try-block, and there has to be a {{rlp|try_catch|catch clause}} that matches the type of the exception object.
 
In order for exception to be caught, the throw-expression has to be inside a {{rlp|try_catch|try-block}} or inside a function called from a try-block, and there has to be a {{rlp|try_catch|catch clause}} that matches the type of the exception object.
 +
 +
When declaring a function, {{rlp|except_spec | exception specifications}} and {{rlp|noexcept_spec | noexcept specifiers}} may be provided to limit the types of the exceptions a function may throw.
 +
 +
Errors that arise during exception handling are handled by {{c|std::terminate}} and {{c|std::unexpected}}.
  
 
===Usage===
 
===Usage===

Revision as of 11:39, 25 March 2013

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 

Exception handling provides a way of transferring control and information from some point in the execution of a program to an handler associated with a point previously passed by the execution (in other words, exception handling transfers control up the call stack)

An exception can be thrown by a throw-expression, and many standard library functions, including memory allocation functions, are specified to throw exceptions to signal certain error conditions.

In order for exception to be caught, the throw-expression has to be inside a try-block or inside a function called from a try-block, and there has to be a catch clause that matches the type of the exception object.

When declaring a function, exception specifications and noexcept specifiers may be provided to limit the types of the exceptions a function may throw.

Errors that arise during exception handling are handled by std::terminate and std::unexpected.

Contents

Usage

While throw-expression can be used to transfer control to an arbitrary block of code up the execution stack, for arbitrary reasons (similar to std::longjmp), its intended usage is error handling.

Error handling

Throwing an exception is used to signal errors from functions, where "errors" are typically limited to only the following[1][2]:

  1. Failures to establish invariants
  2. Failures to meet the postconditions
  3. Failures to meet the preconditions of the caller

In particular, this implies that the failures of constructors and most operators should be reported by throwing exceptions.

Exception safety

After the error condition is reported by a function, additional guarantees may be provided with regards to the state of the program. The following four levels of exception guarantee are generally recognized[3][4][5], which are strict supersets of each other:

  1. Nothrow (or nofail) exception guarantee -- the function never throws exceptions. This is expected of destructors and, typically, of move constructors.
  2. Strong exception guarantee -- If the function throws an exception, the state of the program is rolled back to the state just before the function call.
  3. Basic exception guarantee -- If the function throws an exception, the program is in a valid state. It may require cleanup, but all invariants are intact.
  4. No exception guarantee -- If the function throws an exception, the program may not be in a valid state: resource leaks, memory corruption, or other invariant-destroying errors may have occurred.

Exception objects

While objects of any complete type and cv pointers to void may be thrown as exception objects, all standard library functions throw anonymous temporary objects by value, and the types of those objects are derived (directly or indirectly) from std::exception. User-defined exceptions usually follow this pattern.[6][7]

To avoid unnecessary copying of the exception object and object slicing, the best practice for catch clauses is to catch by reference.[8][9][10]

References

  1. H. Sutter (2004) "When and How to Use Exceptions" in Dr. Dobb's
  2. H.Sutter, A. Alexandrescu (2004), "C++ Coding Standards", Item 70
  3. B. Stroustrup (2000), "The C++ Programming Language"Appendix E"
  4. H. Sutter (2000) "Exceptional C++"
  5. D. Abrahams (2001) "Exception Safety in Generic Components"
  6. D. Abrahams (2001) "Error and Exception Handling"
  7. M. Cline, C++FAQ Lite 17.11
  8. S. Meyers (1996) "More Effective C++" Item 13
  9. M. Cline, C++FAQ Lite 17.12
  10. H.Sutter, A. Alexandrescu (2004) "C++ Coding Standards" Item 73