• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 #include <boost/core/uncaught_exceptions.hpp>
19 #include <boost/core/lightweight_test.hpp>
20 
21 struct my_exception {};
22 
23 class exception_watcher
24 {
25     unsigned int& m_count;
26 
27 public:
exception_watcher(unsigned int & count)28     explicit exception_watcher(unsigned int& count) : m_count(count) {}
~exception_watcher()29     ~exception_watcher() { m_count = boost::core::uncaught_exceptions(); }
30 };
31 
32 // Tests for uncaught_exceptions when used in a destructor while an exception propagates
test_in_destructor()33 void test_in_destructor()
34 {
35     const unsigned int root_count = boost::core::uncaught_exceptions();
36 
37     unsigned int level1_count = root_count;
38     try
39     {
40         exception_watcher watcher(level1_count);
41         throw my_exception();
42     }
43     catch (...)
44     {
45     }
46 
47     BOOST_TEST_NE(root_count, level1_count);
48 }
49 
main()50 int main()
51 {
52     test_in_destructor();
53 
54     return boost::report_errors();
55 }
56