rethrow_if_nested.pass.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. friend bool operator==(const A& x, const A& y) {return x.data_ == y.data_;}
  21. };
  22. class B
  23. : public std::nested_exception
  24. {
  25. int data_;
  26. public:
  27. explicit B(int data) : data_(data) {}
  28. B(const B& b) : data_(b.data_) {}
  29. friend bool operator==(const B& x, const B& y) {return x.data_ == y.data_;}
  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& b0)
  51. {
  52. try
  53. {
  54. B b = b0;
  55. std::rethrow_if_nested(b);
  56. assert(false);
  57. }
  58. catch (const B& b)
  59. {
  60. assert(b == B(5));
  61. }
  62. }
  63. }
  64. }