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 
9 // UNSUPPORTED: libcpp-no-exceptions
10 // <exception>
11 
12 // class nested_exception;
13 
14 // void rethrow_nested [[noreturn]] () const;
15 
16 #include <exception>
17 #include <cstdlib>
18 #include <cassert>
19 
20 class A
21 {
22     int data_;
23 public:
24     explicit A(int data) : data_(data) {}
25 
26     friend bool operator==(const A& x, const A& y) {return x.data_ == y.data_;}
27 };
28 
29 void go_quietly()
30 {
31     std::exit(0);
32 }
33 
34 int main()
35 {
36     {
37         try
38         {
39             throw A(2);
40             assert(false);
41         }
42         catch (const A&)
43         {
44             const std::nested_exception e;
45             assert(e.nested_ptr() != nullptr);
46             try
47             {
48                 e.rethrow_nested();
49                 assert(false);
50             }
51             catch (const A& a)
52             {
53                 assert(a == A(2));
54             }
55         }
56     }
57     {
58         try
59         {
60             std::set_terminate(go_quietly);
61             const std::nested_exception e;
62             e.rethrow_nested();
63             assert(false);
64         }
65         catch (...)
66         {
67             assert(false);
68         }
69     }
70 }
71