uncaught_exceptions.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright Andrey Semashev 2018.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * https://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file uncaught_exceptions.cpp
  9. * \author Andrey Semashev
  10. * \date 2018-11-10
  11. *
  12. * \brief This file contains tests for the uncaught_exceptions function.
  13. *
  14. * This file only contains the very basic checks of functionality that can be portably achieved
  15. * through C++03 std::uncaught_exception.
  16. */
  17. #include <boost/core/uncaught_exceptions.hpp>
  18. #include <boost/core/lightweight_test.hpp>
  19. struct my_exception {};
  20. class exception_watcher
  21. {
  22. unsigned int& m_count;
  23. public:
  24. explicit exception_watcher(unsigned int& count) : m_count(count) {}
  25. ~exception_watcher() { m_count = boost::core::uncaught_exceptions(); }
  26. };
  27. // Tests for uncaught_exceptions when used in a destructor while an exception propagates
  28. void test_in_destructor()
  29. {
  30. const unsigned int root_count = boost::core::uncaught_exceptions();
  31. unsigned int level1_count = root_count;
  32. try
  33. {
  34. exception_watcher watcher(level1_count);
  35. throw my_exception();
  36. }
  37. catch (...)
  38. {
  39. }
  40. BOOST_TEST_NE(root_count, level1_count);
  41. }
  42. int main()
  43. {
  44. test_in_destructor();
  45. return boost::report_errors();
  46. }