rethrow_nested.pass.cpp 1.4 KB

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