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