Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/error/exception/exception"

From cppreference.com
< cpp‎ | error‎ | exception
(note about COW strings in the implementations.)
(Added LWG issue #471 DR (part 2/3).)
 
(5 intermediate revisions by 5 users not shown)
Line 2: Line 2:
 
{{cpp/error/exception/navbar}}
 
{{cpp/error/exception/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl | num=1 |
+
{{dcl rev multi|num=1|until1=c++11|dcl1=
exception();
+
exception() throw();
 +
|dcl2=
 +
exception() noexcept;
 
}}
 
}}
{{dcl | num=2 |  
+
{{dcl rev multi|num=2|until1=c++11|dcl1=
exception( const exception& other );
+
exception( const exception& other ) throw();
 +
|dcl2=
 +
exception( const exception& other ) noexcept;
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
Line 12: Line 16:
 
Constructs new exception object.
 
Constructs new exception object.
  
@1@ Default constructor. {{ltf|cpp/error/exception/what}} returns empty string.
+
@1@ Default constructor. {{lc|what()}} returns an implementation-defined string.
  
@2@ Copy constructor. Initializes the contents with those of {{tt|other}}
+
@2@ Copy constructor. Initializes the contents with those of {{c|other}}. If {{c|*this}} and {{c|other}} both have dynamic type {{tt|std::exception}} then {{c|1=std::strcmp(what(), other.what()) == 0}}.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | other | another exception to assign the contents of}}
+
{{par|other|another exception to assign the contents of}}
 
{{par end}}
 
{{par end}}
 
===Exceptions===
 
{{rev begin}}
 
{{rev | until=c++11 | (none)}}
 
{{rev | since=c++11 | {{noexcept}}}}
 
{{rev end}}
 
  
 
===Notes===
 
===Notes===
Because copying {{tt|std::exception}} is not permitted to throw exceptions, when derived classes (such as {{lc|std:runtime_error}}) have to manage a user-defined diagnostic message, it is typically implemented as a copy-on-write string.
+
Because copying {{tt|std::exception}} is not permitted to throw exceptions, when derived classes (such as {{lc|std::runtime_error}}) have to manage a user-defined diagnostic message, it is typically implemented as a copy-on-write string.
 +
 
 +
The Microsoft implementation includes non-standard constructors taking strings thus allowing instances to be thrown directly with a meaningful error message. The nearest standard equivalents are {{lc|std::runtime_error}} or {{lc|std::logic_error}}.
 +
 
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=471|std=C++98|before=there is no requirement on {{lc|what()}} of the exception copy|after=added}}
 +
{{dr list end}}
  
[[de:cpp/error/exception/exception]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/error/exception/exception]]
+
[[fr:cpp/error/exception/exception]]
+
[[it:cpp/error/exception/exception]]
+
[[ja:cpp/error/exception/exception]]
+
[[pt:cpp/error/exception/exception]]
+
[[ru:cpp/error/exception/exception]]
+
[[zh:cpp/error/exception/exception]]
+

Latest revision as of 18:05, 20 February 2023

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
 
(1)
exception() throw();
(until C++11)
exception() noexcept;
(since C++11)
(2)
exception( const exception& other ) throw();
(until C++11)
exception( const exception& other ) noexcept;
(since C++11)

Constructs new exception object.

1) Default constructor. what() returns an implementation-defined string.
2) Copy constructor. Initializes the contents with those of other. If *this and other both have dynamic type std::exception then std::strcmp(what(), other.what()) == 0.

[edit] Parameters

other - another exception to assign the contents of

[edit] Notes

Because copying std::exception is not permitted to throw exceptions, when derived classes (such as std::runtime_error) have to manage a user-defined diagnostic message, it is typically implemented as a copy-on-write string.

The Microsoft implementation includes non-standard constructors taking strings thus allowing instances to be thrown directly with a meaningful error message. The nearest standard equivalents are std::runtime_error or std::logic_error.

[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 is no requirement on what() of the exception copy added