rethrow_nested.pass.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // UNSUPPORTED: libcpp-no-exceptions
  9. // <exception>
  10. // class nested_exception;
  11. // void rethrow_nested [[noreturn]] () const;
  12. #include <exception>
  13. #include <cstdlib>
  14. #include <cassert>
  15. #include "test_macros.h"
  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(int, char**)
  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. return 0;
  64. }