rethrow_if_nested.pass.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // XFAIL: libcpp-no-exceptions
  10. // <exception>
  11. // class nested_exception;
  12. // template <class E> void rethrow_if_nested(const E& e);
  13. #include <exception>
  14. #include <cstdlib>
  15. #include <cassert>
  16. class A
  17. {
  18. int data_;
  19. public:
  20. explicit A(int data) : data_(data) {}
  21. virtual ~A() _NOEXCEPT {}
  22. friend bool operator==(const A& x, const A& y) {return x.data_ == y.data_;}
  23. };
  24. class B
  25. : public std::nested_exception,
  26. public A
  27. {
  28. public:
  29. explicit B(int data) : A(data) {}
  30. B(const B& b) : A(b) {}
  31. };
  32. int main()
  33. {
  34. {
  35. try
  36. {
  37. A a(3);
  38. std::rethrow_if_nested(a);
  39. assert(true);
  40. }
  41. catch (...)
  42. {
  43. assert(false);
  44. }
  45. }
  46. {
  47. try
  48. {
  49. throw B(5);
  50. }
  51. catch (const B& b)
  52. {
  53. try
  54. {
  55. throw b;
  56. }
  57. catch (const A& a)
  58. {
  59. try
  60. {
  61. std::rethrow_if_nested(a);
  62. assert(false);
  63. }
  64. catch (const B& b)
  65. {
  66. assert(b == B(5));
  67. }
  68. }
  69. }
  70. }
  71. {
  72. try
  73. {
  74. std::rethrow_if_nested(1);
  75. assert(true);
  76. }
  77. catch (...)
  78. {
  79. assert(false);
  80. }
  81. }
  82. }