rethrow_if_nested.pass.cpp 1.7 KB

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