Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/error/exception"

From cppreference.com
< cpp‎ | error
m (r2.7.3) (Robot: Adding fr, it, ja, zh)
(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...)
 
(30 intermediate revisions by 9 users not shown)
Line 1: Line 1:
{{cpp/title | exception}}
+
{{cpp/title|exception}}
{{cpp/error/exception/sidebar}}
+
{{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]].  
  
Most of the exception classes inherit from {{c|std::exception}}
+
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/logic_error}}
 
:*{{ltt|cpp/error/invalid_argument}}
 
:*{{ltt|cpp/error/invalid_argument}}
Line 14: Line 34:
 
:*{{ltt|cpp/error/length_error}}
 
:*{{ltt|cpp/error/length_error}}
 
:*{{ltt|cpp/error/out_of_range}}
 
:*{{ltt|cpp/error/out_of_range}}
 +
:*{{ltt|cpp/thread/future_error}} {{mark since c++11}}
 
*{{ltt|cpp/error/runtime_error}}
 
*{{ltt|cpp/error/runtime_error}}
 
:*{{ltt|cpp/error/range_error}}
 
:*{{ltt|cpp/error/range_error}}
 
:*{{ltt|cpp/error/overflow_error}}
 
:*{{ltt|cpp/error/overflow_error}}
 
:*{{ltt|cpp/error/underflow_error}}
 
:*{{ltt|cpp/error/underflow_error}}
*{{ltt|cpp/io/ios_base/failure|ios_base::failure}}
+
:*{{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_typeid}}
 
*{{ltt|cpp/types/bad_cast}}
 
*{{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_alloc}}
:*{{ltt|cpp/memory/new/bad_array_new_length}}
+
:*{{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}}
  
===Member functions===
+
===Defect reports===
{{dcl list begin}}
+
{{dr list begin}}
{{dcl list template | cpp/error/exception/dcl list constructor | exception}}
+
{{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}}
{{dcl list template | cpp/error/exception/dcl list destructor}}
+
{{dr list end}}
{{dcl list template | cpp/error/exception/dcl list operator{{=}}}}
+
{{dcl list template | cpp/error/exception/dcl list what}}
+
{{tdcl list end}}
+
  
[[fr:cpp/error/exception]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[it:cpp/error/exception]]
+
[[ja:cpp/error/exception]]
+
[[zh:cpp/error/exception]]
+

Latest revision as of 19:17, 7 May 2024

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
 
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) [edit]
copies exception object
(public member function) [edit]
[virtual]
returns an explanatory string
(virtual public member function) [edit]

[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):

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

[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