82957de970
The uncaught_exceptions function is functionally equivalent to unhandled_exceptions_count in Boost.Log and implements functionality similar to the same named C++17 standard function. Tests and docs are also included. One notable difference from std::uncaught_exceptions is that the return type is unsigned rather than signed. This is deliberate as uncaught_exceptions must never return a negative value and unsigned int better documents that. Theoretically, as a counter, it may also overflow.
53 lines
1.5 KiB
Plaintext
53 lines
1.5 KiB
Plaintext
[/
|
|
/ Copyright (c) 2018 Andrey Semashev
|
|
/
|
|
/ Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
/ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
/]
|
|
|
|
[section:uncaught_exceptions uncaught_exceptions]
|
|
|
|
[simplesect Authors]
|
|
|
|
* Andrey Semashev
|
|
|
|
[endsimplesect]
|
|
|
|
[section Header <boost/core/uncaught_exceptions.hpp>]
|
|
|
|
The header `<boost/core/uncaught_exceptions.hpp>` defines the `boost::core::uncaught_exceptions` function,
|
|
which is a more portable implementation of the same named function introduced in C++17. The function
|
|
returns the number of the currently pending exceptions. When that function returns a value greater than 0,
|
|
throwing an exception from a destructor can terminate the program.
|
|
|
|
Unfortunately, the function cannot be implemented on every pre-C++17 compiler, although the most commonly
|
|
used compilers are supported. When the compiler does not provide the necessary functionality,
|
|
`boost::core::uncaught_exceptions` returns a non-zero value if at least one exception is pending (i.e. not
|
|
necessarily the number of pending exceptions), and `BOOST_CORE_UNCAUGHT_EXCEPTIONS_EMULATED` macro
|
|
is defined.
|
|
|
|
[section Example]
|
|
``
|
|
class my_class
|
|
{
|
|
private:
|
|
const unsigned int m_exception_count;
|
|
|
|
public:
|
|
my_class() : m_exception_count(boost::core::uncaught_exceptions())
|
|
{
|
|
}
|
|
|
|
~my_class() noexcept(false)
|
|
{
|
|
if (m_exception_count == boost::core::uncaught_exceptions())
|
|
do_something_potentially_throwing();
|
|
}
|
|
};
|
|
``
|
|
[endsect]
|
|
|
|
[endsect]
|
|
|
|
[endsect]
|