std::uncaug..._exceptions(3)
π std::uncaught_exception,std::uncaught_exceptions π
okay bestie, welcome to the C++ Standard Libary manual page for std::uncaught_exception,std::uncaught_exceptions β manual section (3), and the page header lowkey abbreviates it as std::uncaug..._exceptions(3). yes, the header spells it "Libary"; we love a typo moment. this page is all about knowing whether stack unwinding is happening and how many exceptions are currently uncaught, and it's giving destructor safety vibes β¨
π·οΈ NAME π·οΈ
std::uncaught_exception,std::uncaught_exceptions β literally the whole name, bestie, comma and all. π
β¨ Synopsis β¨
Defined in header <exception>. The signatures have eras, so keep up:
bool uncaught_exception() throw(); (until C++11)
(since C++11)
bool uncaught_exception() noexcept; (1) (deprecated in C++17)
(removed in C++20)
int uncaught_exceptions() noexcept; (2) (since C++17)
(1)β Detects if the current thread has a live exception object, that is, an exception has been thrown or rethrown and not yet entered a matchingcatchclause,std::terminateorstd::unexpected. In other words,std::uncaught_exceptiondetects if stack unwinding is currently in progress.(2)β Detects how many exceptions in the current thread have been thrown or rethrown and not yet entered their matchingcatchclauses.
Sometimes it's safe to throw an exception even while std::uncaught_exception() == true. For example, if stack unwinding causes an object to be destructed, the destructor for that object could run code that throws an exception as long as the exception is caught by some catch block before escaping the destructor. important caveat tea π«
ποΈ Parameters ποΈ
(none) β literally none, bestie. no arguments, no stress. π
π Return value π
trueif stack unwinding is currently in progress in this thread,falseotherwise.- The number of uncaught exception objects in the current thread.
π Notes π
An example where the int-returning uncaught_exceptions is used is the boost.log library: the expression BOOST_LOG(logger) << foo(); first creates a guard object and records the number of uncaught exceptions in its constructor. The output is performed by the guard object's destructor unless foo() throws (in which case the number of uncaught exceptions in the destructor is greater than what the constructor observed).
std::experimental::scope_fail and std::experimental::scope_success in LFTS v3 rely on the functionality of uncaught_exceptions, because their destructors need to do different things that depend on whether it is called during stack unwinding.
Feature-test macro tea: π΅
__cpp_lib_uncaught_exceptionsβ value201411L, Std(C++17), featurestd::uncaught_exceptions.
π» Example π»
// Run this code
#include <exception>
#include <iostream>
#include <stdexcept>
struct Foo
{
char id{'?'};
int count = std::uncaught_exceptions();
~Foo()
{
count == std::uncaught_exceptions()
? std::cout << id << ".~Foo() called normally\n"
: std::cout << id << ".~Foo() called during stack unwinding\n";
}
};
int main()
{
Foo f{'f'};
try
{
Foo g{'g'};
std::cout << "Exception thrown\n";
throw std::runtime_error("test exception");
}
catch (const std::exception& e)
{
std::cout << "Exception caught: " << e.what() << '\n';
}
}
π₯οΈ Possible output π₯οΈ
Exception thrown
g.~Foo() called during stack unwinding
Exception caught: test exception
f.~Foo() called normally
π οΈ Defect reports π οΈ
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR
LWG 70β applied to C++98; behavior as published: the exception specification ofuncaught_exception()was missing; correct behavior: specified asthrow().
π See also π
terminateβ function called when exception handling fails(function)exception_ptrβ shared pointer type for handling exception objects(C++11)(typedef)current_exceptionβ captures the current exception in astd::exception_ptr(C++11)(function)
π External links π
- GOTW issue 47: Uncaught Exceptions
- Rationale for
std::uncaught_exceptions(N4125)
okay that's the whole page, bestie β source: http://cppreference.com, version/date: 2024.06.10 πβ¨π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms