Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/error/unexpected"

From cppreference.com
< cpp‎ | error
m
 
(7 intermediate revisions by 6 users not shown)
Line 2: Line 2:
 
{{cpp/error/navbar}}
 
{{cpp/error/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | exception}}
+
{{dcl header|exception}}
 
{{dcl rev begin}}
 
{{dcl rev begin}}
{{dcl | until=c++11 |
+
{{dcl|until=c++11|
 
void unexpected();
 
void unexpected();
 
}}
 
}}
{{dcl | since=c++11 | deprecated=yes | until=c++17 |
+
{{dcl|deprecated=c++11|until=c++17|
 
[[noreturn]] void unexpected();
 
[[noreturn]] void unexpected();
 
}}
 
}}
Line 13: Line 13:
 
{{dcl end}}
 
{{dcl end}}
  
{{tt|std::unexpected()}} is called by the C++ runtime when a [[cpp/language/except_spec|dynamic exception specification]] is violated: an exception is thrown from a function whose exception specification forbids exceptions of this type.
+
{{tt|std::unexpected()}} is called by the C++ runtime when a [[cpp/language/except spec|dynamic exception specification]] is violated: an exception is thrown from a function whose exception specification forbids exceptions of this type.
  
 
{{tt|std::unexpected()}} may also be called directly from the program.
 
{{tt|std::unexpected()}} may also be called directly from the program.
Line 21: Line 21:
 
{{rev begin}}
 
{{rev begin}}
 
{{rev|until=c++11|
 
{{rev|until=c++11|
If a destructor reset the unexpected handler during stack unwinding and the unwinding later led to {{tt|unexpected}} being called, the handler that was installed at the end of the throw expression is the one that will be called. (note: it was ambiguous whether re-throwing applied the new handlers)
+
If a destructor reset the unexpected handler during stack unwinding and the unwinding later led to {{tt|unexpected}} being called, the handler that was installed at the end of the throw expression is the one that will be called (note: it was ambiguous whether re-throwing applied the new handlers).
 
}}
 
}}
{{rev|since=c++17|
+
{{rev|since=c++11|
 
If a destructor reset the unexpected handler during stack unwinding, it is unspecified which handler is called if the unwinding later led to {{tt|unexpected}} being called.
 
If a destructor reset the unexpected handler during stack unwinding, it is unspecified which handler is called if the unwinding later led to {{tt|unexpected}} being called.
 
}}
 
}}
 
{{rev end}}
 
{{rev end}}
  
 +
===Exceptions===
 +
Throw any exception thrown by the currently installed {{lc|std::unexpected_handler}}.
  
===Parameters===
+
===Defect reports===
(none)
+
{{dr list begin}}
 
+
{{dr list item|wg=lwg|dr=2111|std=C++11|before=effect of calling {{lc|std::set_unexpected}} during stack<br>unwinding differs from C++98 and breaks some ABIs|after=made unspecified}}
===Return value===
+
{{dr list end}}
(none)
+
 
+
===Exceptions===
+
Throw any exception thrown by the currently installed {{lc|std::unexpected_handler}}
+
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/error/dsc unexpected_handler}}
+
{{dsc inc|cpp/utility/expected/dsc unexpected}}
 +
{{dsc inc|cpp/error/dsc unexpected_handler}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/error/unexpected]]
+
{{langlinks|de|es|fr|it|ja|ru|zh}}
[[es:cpp/error/unexpected]]
+
[[fr:cpp/error/unexpected]]
+
[[it:cpp/error/unexpected]]
+
[[ja:cpp/error/unexpected]]
+
[[pt:cpp/error/unexpected]]
+
[[ru:cpp/error/unexpected]]
+
[[zh:cpp/error/unexpected]]
+

Latest revision as of 03:48, 20 August 2024

 
 
 
Defined in header <exception>
void unexpected();
(until C++11)
[[noreturn]] void unexpected();
(deprecated in C++11)
(removed in C++17)

std::unexpected() is called by the C++ runtime when a dynamic exception specification is violated: an exception is thrown from a function whose exception specification forbids exceptions of this type.

std::unexpected() may also be called directly from the program.

In either case, std::unexpected calls the currently installed std::unexpected_handler. The default std::unexpected_handler calls std::terminate.

If a destructor reset the unexpected handler during stack unwinding and the unwinding later led to unexpected being called, the handler that was installed at the end of the throw expression is the one that will be called (note: it was ambiguous whether re-throwing applied the new handlers).

(until C++11)

If a destructor reset the unexpected handler during stack unwinding, it is unspecified which handler is called if the unwinding later led to unexpected being called.

(since C++11)

[edit] Exceptions

Throw any exception thrown by the currently installed std::unexpected_handler.

[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 2111 C++11 effect of calling std::set_unexpected during stack
unwinding differs from C++98 and breaks some ABIs
made unspecified

[edit] See also

represented as an unexpected value
(class template) [edit]
(deprecated in C++11)(removed in C++17)
the type of the function called by std::unexpected
(typedef) [edit]