uncaught_exceptions.qbk 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. [/
  2. / Copyright (c) 2018 Andrey Semashev
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [section:uncaught_exceptions uncaught_exceptions]
  8. [simplesect Authors]
  9. * Andrey Semashev
  10. [endsimplesect]
  11. [section Header <boost/core/uncaught_exceptions.hpp>]
  12. The header `<boost/core/uncaught_exceptions.hpp>` defines the `boost::core::uncaught_exceptions` function,
  13. which is a more portable implementation of the same named function introduced in C++17. The function
  14. returns the number of the currently pending exceptions. When that function returns a value greater than 0,
  15. throwing an exception from a destructor can terminate the program.
  16. Unfortunately, the function cannot be implemented on every pre-C++17 compiler, although the most commonly
  17. used compilers are supported. When the compiler does not provide the necessary functionality,
  18. `boost::core::uncaught_exceptions` returns a non-zero value if at least one exception is pending (i.e. not
  19. necessarily the number of pending exceptions), and `BOOST_CORE_UNCAUGHT_EXCEPTIONS_EMULATED` macro
  20. is defined.
  21. [section Example]
  22. ``
  23. class my_class
  24. {
  25. private:
  26. const unsigned int m_exception_count;
  27. public:
  28. my_class() : m_exception_count(boost::core::uncaught_exceptions())
  29. {
  30. }
  31. ~my_class() noexcept(false)
  32. {
  33. if (m_exception_count == boost::core::uncaught_exceptions())
  34. do_something_potentially_throwing();
  35. }
  36. };
  37. ``
  38. [endsect]
  39. [endsect]
  40. [endsect]