Difference between revisions of "cpp/error/exception"
From cppreference.com
(→Standard exception requirements: Changed only by https://github.com/cplusplus/draft/pull/1867, not any WG21 paper. Let's try to treat it as DR...) |
|||
(41 intermediate revisions by 9 users not shown) | |||
Line 1: | Line 1: | ||
− | {{cpp/title | exception}} | + | {{cpp/title|exception}} |
− | {{cpp/error/exception/ | + | {{cpp/error/exception/navbar}} |
− | {{ddcl | header=exception | | + | {{ddcl|header=exception| |
class exception; | class exception; | ||
}} | }} | ||
Line 7: | Line 7: | ||
Provides consistent interface to handle errors through the [[cpp/language/throw|throw expression]]. | Provides consistent interface to handle errors through the [[cpp/language/throw|throw expression]]. | ||
− | {{ | + | All exceptions generated by the standard library inherit from {{tt|std::exception}}. |
− | {{ | + | |
− | {{ | + | ===Member functions=== |
− | {{ | + | {{dsc begin}} |
− | {{ | + | {{dsc mem ctor|cpp/error/exception/exception|constructs the exception object}} |
− | {{ | + | {{dsc inc|cpp/error/exception/dsc destructor}} |
+ | {{dsc inc|cpp/error/exception/dsc operator{{=}}}} | ||
+ | {{dsc inc|cpp/error/exception/dsc what}} | ||
+ | {{dsc end}} | ||
+ | |||
+ | ===Standard exception requirements=== | ||
+ | Each standard library class {{tt|T}} that derives from {{tt|std::exception}} has the following publicly accessible member functions, each of them {{rev inl|until=c++11|do not exit with an exception}}{{rev inl|since=c++11|having a [[cpp/language/noexcept spec|non-throwing exception specification]]}}: | ||
+ | * {{lt|cpp/language/default constructor}} (unless other constructors are provided) | ||
+ | * {{lt|cpp/language/copy constructor}} | ||
+ | * [[cpp/language/copy assignment|copy assignment operator]] | ||
+ | |||
+ | The copy constructor and the copy assignment operator meet the following postcondition: | ||
+ | * If two objects {{c|lhs}} and {{c|rhs}} both have dynamic type {{tt|T}} and {{c|lhs}} is a copy of {{c|rhs}}, then {{c|std::strcmp(lhs.what(), rhs.what())}} is equal to {{c|0}}. | ||
+ | |||
+ | The {{tt|what()}} member function of each such {{tt|T}} satisfies the constraints specified for {{lc|std::exception::what()}}. | ||
+ | |||
+ | ===Standard exceptions=== | ||
+ | *{{ltt|cpp/error/logic_error}} | ||
+ | :*{{ltt|cpp/error/invalid_argument}} | ||
+ | :*{{ltt|cpp/error/domain_error}} | ||
+ | :*{{ltt|cpp/error/length_error}} | ||
+ | :*{{ltt|cpp/error/out_of_range}} | ||
+ | :*{{ltt|cpp/thread/future_error}} {{mark since c++11}} | ||
+ | *{{ltt|cpp/error/runtime_error}} | ||
+ | :*{{ltt|cpp/error/range_error}} | ||
+ | :*{{ltt|cpp/error/overflow_error}} | ||
+ | :*{{ltt|cpp/error/underflow_error}} | ||
+ | :*{{ltt|cpp/regex/regex_error}} {{mark since c++11}} | ||
+ | :*{{ltt|cpp/error/system_error}} {{mark since c++11}} | ||
+ | ::*{{l2tt|cpp/io/ios_base/failure}} {{mark since c++11}} | ||
+ | ::*{{l2tt|cpp/filesystem/filesystem_error}} {{mark since c++17}} | ||
+ | :*{{ltt|cpp/error/tx_exception}} {{mark since tm ts}} | ||
+ | :*{{ltt|cpp/chrono/nonexistent_local_time}} {{mark since c++20}} | ||
+ | :*{{ltt|cpp/chrono/ambiguous_local_time}} {{mark since c++20}} | ||
+ | :*{{ltt|cpp/utility/format/format_error}} {{mark since c++20}} | ||
+ | *{{ltt|cpp/types/bad_typeid}} | ||
+ | *{{ltt|cpp/types/bad_cast}} | ||
+ | :*{{ltt|cpp/utility/any/bad_any_cast}} {{mark since c++17}} | ||
+ | *{{ltt|cpp/utility/optional/bad_optional_access}} {{mark since c++17}} | ||
+ | *{{ltt|cpp/utility/expected/bad_expected_access}} {{mark since c++23}} | ||
+ | *{{ltt|cpp/memory/bad_weak_ptr}} {{mark since c++11}} | ||
+ | *{{ltt|cpp/utility/functional/bad_function_call}} {{mark since c++11}} | ||
+ | *{{ltt|cpp/memory/new/bad_alloc}} | ||
+ | :*{{ltt|cpp/memory/new/bad_array_new_length}} {{mark since c++11}} | ||
+ | *{{ltt|cpp/error/bad_exception}} | ||
+ | *{{l2tt|cpp/io/ios_base/failure}} {{mark until c++11}} | ||
+ | *{{ltt|cpp/utility/variant/bad_variant_access}} {{mark since c++17}} | ||
+ | |||
+ | ===Defect reports=== | ||
+ | {{dr list begin}} | ||
+ | {{dr list item|wg=lwg|dr=471|std=C++98|before=there was no requirement on standard library classes derived from {{tt|std::exception}}|after=added}} | ||
+ | {{dr list end}} | ||
+ | |||
+ | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
Latest revision as of 19:17, 7 May 2024
Defined in header <exception>
|
||
class exception; |
||
Provides consistent interface to handle errors through the throw expression.
All exceptions generated by the standard library inherit from std::exception
.
Contents |
[edit] Member functions
constructs the exception object (public member function) | |
[virtual] |
destroys the exception object (virtual public member function) |
copies exception object (public member function) | |
[virtual] |
returns an explanatory string (virtual public member function) |
[edit] Standard exception requirements
Each standard library class T
that derives from std::exception
has the following publicly accessible member functions, each of them do not exit with an exception(until C++11)having a non-throwing exception specification(since C++11):
- default constructor (unless other constructors are provided)
- copy constructor
- copy assignment operator
The copy constructor and the copy assignment operator meet the following postcondition:
- If two objects lhs and rhs both have dynamic type
T
and lhs is a copy of rhs, then std::strcmp(lhs.what(), rhs.what()) is equal to 0.
The what()
member function of each such T
satisfies the constraints specified for std::exception::what().
[edit] Standard exceptions
- invalid_argument
- domain_error
- length_error
- out_of_range
- future_error (since C++11)
- range_error
- overflow_error
- underflow_error
- regex_error (since C++11)
- system_error (since C++11)
- ios_base::failure (since C++11)
- filesystem::filesystem_error (since C++17)
- tx_exception (TM TS)
- nonexistent_local_time (since C++20)
- ambiguous_local_time (since C++20)
- format_error (since C++20)
- bad_any_cast (since C++17)
- bad_optional_access (since C++17)
- bad_expected_access (since C++23)
- bad_weak_ptr (since C++11)
- bad_function_call (since C++11)
- bad_alloc
- bad_array_new_length (since C++11)
- bad_exception
- ios_base::failure (until C++11)
- bad_variant_access (since C++17)
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 471 | C++98 | there was no requirement on standard library classes derived from std::exception
|
added |