throw_exception_test3.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2018 Peter Dimov
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. //
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. #include <boost/throw_exception.hpp>
  8. #include <boost/exception_ptr.hpp>
  9. #include <boost/detail/lightweight_test.hpp>
  10. class my_exception: public std::exception
  11. {
  12. };
  13. class my_exception2: public std::exception, public boost::exception
  14. {
  15. };
  16. int main()
  17. {
  18. try
  19. {
  20. boost::throw_exception( my_exception() );
  21. }
  22. catch( ... )
  23. {
  24. boost::exception_ptr p = boost::current_exception();
  25. BOOST_TEST_THROWS( boost::rethrow_exception( p ), my_exception );
  26. BOOST_TEST_THROWS( boost::rethrow_exception( p ), boost::exception );
  27. }
  28. try
  29. {
  30. boost::throw_exception( my_exception2() );
  31. }
  32. catch( ... )
  33. {
  34. boost::exception_ptr p = boost::current_exception();
  35. BOOST_TEST_THROWS( boost::rethrow_exception( p ), my_exception2 );
  36. BOOST_TEST_THROWS( boost::rethrow_exception( p ), boost::exception );
  37. }
  38. try
  39. {
  40. BOOST_THROW_EXCEPTION( my_exception() );
  41. }
  42. catch( ... )
  43. {
  44. boost::exception_ptr p = boost::current_exception();
  45. BOOST_TEST_THROWS( boost::rethrow_exception( p ), my_exception );
  46. BOOST_TEST_THROWS( boost::rethrow_exception( p ), boost::exception );
  47. }
  48. try
  49. {
  50. BOOST_THROW_EXCEPTION( my_exception2() );
  51. }
  52. catch( ... )
  53. {
  54. boost::exception_ptr p = boost::current_exception();
  55. BOOST_TEST_THROWS( boost::rethrow_exception( p ), my_exception2 );
  56. BOOST_TEST_THROWS( boost::rethrow_exception( p ), boost::exception );
  57. }
  58. return boost::report_errors();
  59. }