rethrow_nested.pass.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. // UNSUPPORTED: libcpp-no-exceptions
  10. // <exception>
  11. // class nested_exception;
  12. // void rethrow_nested [[noreturn]] () const;
  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. friend bool operator==(const A& x, const A& y) {return x.data_ == y.data_;}
  22. };
  23. void go_quietly()
  24. {
  25. std::exit(0);
  26. }
  27. int main()
  28. {
  29. {
  30. try
  31. {
  32. throw A(2);
  33. assert(false);
  34. }
  35. catch (const A&)
  36. {
  37. const std::nested_exception e;
  38. assert(e.nested_ptr() != nullptr);
  39. try
  40. {
  41. e.rethrow_nested();
  42. assert(false);
  43. }
  44. catch (const A& a)
  45. {
  46. assert(a == A(2));
  47. }
  48. }
  49. }
  50. {
  51. try
  52. {
  53. std::set_terminate(go_quietly);
  54. const std::nested_exception e;
  55. e.rethrow_nested();
  56. assert(false);
  57. }
  58. catch (...)
  59. {
  60. assert(false);
  61. }
  62. }
  63. }