unknown_exception_test.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.
  2. //Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #include <boost/exception_ptr.hpp>
  5. #include <boost/exception/get_error_info.hpp>
  6. #include <boost/exception/info.hpp>
  7. #include <boost/detail/lightweight_test.hpp>
  8. #include <boost/detail/workaround.hpp>
  9. #if BOOST_WORKAROUND(__CODEGEARC__, BOOST_TESTED_AT(0x610))
  10. struct tag_test {};
  11. #endif
  12. typedef boost::error_info<struct tag_test,int> test;
  13. struct
  14. test_boost_exception:
  15. boost::exception
  16. {
  17. };
  18. void
  19. throw_boost_exception()
  20. {
  21. throw test_boost_exception() << test(42);
  22. }
  23. void
  24. throw_unknown_exception()
  25. {
  26. struct
  27. test_exception:
  28. std::exception
  29. {
  30. };
  31. throw test_exception();
  32. }
  33. int
  34. main()
  35. {
  36. try
  37. {
  38. throw_boost_exception();
  39. }
  40. catch(
  41. ... )
  42. {
  43. boost::exception_ptr ep=boost::current_exception();
  44. try
  45. {
  46. rethrow_exception(ep);
  47. }
  48. catch(
  49. boost::unknown_exception & x )
  50. {
  51. if( int const * d=boost::get_error_info<test>(x) )
  52. BOOST_TEST( 42==*d );
  53. else
  54. BOOST_TEST(false);
  55. }
  56. catch(
  57. boost::exception & x )
  58. {
  59. //Yay! Non-intrusive cloning supported!
  60. if( int const * d=boost::get_error_info<test>(x) )
  61. BOOST_TEST( 42==*d );
  62. else
  63. BOOST_TEST(false);
  64. }
  65. catch(
  66. ... )
  67. {
  68. BOOST_TEST(false);
  69. }
  70. try
  71. {
  72. rethrow_exception(ep);
  73. }
  74. catch(
  75. boost::exception & x )
  76. {
  77. if( int const * d=boost::get_error_info<test>(x) )
  78. BOOST_TEST( 42==*d );
  79. else
  80. BOOST_TEST(false);
  81. }
  82. catch(
  83. ... )
  84. {
  85. BOOST_TEST(false);
  86. }
  87. }
  88. try
  89. {
  90. throw_unknown_exception();
  91. }
  92. catch(
  93. ... )
  94. {
  95. boost::exception_ptr ep=boost::current_exception();
  96. try
  97. {
  98. rethrow_exception(ep);
  99. }
  100. catch(
  101. boost::unknown_exception & )
  102. {
  103. }
  104. catch(
  105. std::exception & )
  106. {
  107. //Yay! Non-intrusive cloning supported!
  108. }
  109. catch(
  110. ... )
  111. {
  112. BOOST_TEST(false);
  113. }
  114. try
  115. {
  116. rethrow_exception(ep);
  117. }
  118. catch(
  119. boost::exception & )
  120. {
  121. }
  122. catch(
  123. std::exception & )
  124. {
  125. //Yay! Non-intrusive cloning supported!
  126. }
  127. catch(
  128. ... )
  129. {
  130. BOOST_TEST(false);
  131. }
  132. }
  133. return boost::report_errors();
  134. }