]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/18_support/nested_exception/rethrow_if_nested.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 18_support / nested_exception / rethrow_if_nested.cc
CommitLineData
be58e01d 1// { dg-do run { target c++11 } }
ec206094 2
fbd26352 3// Copyright (C) 2009-2019 Free Software Foundation, Inc.
ec206094 4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License along
17// with this library; see the file COPYING3. If not see
18// <http://www.gnu.org/licenses/>.
19
20#include <exception>
21#include <testsuite_hooks.h>
22
23struct derived : std::nested_exception { };
24
d2b14329 25struct base { virtual ~base() noexcept; };
26inline base::~base() noexcept = default;
ec206094 27
28struct derived2 : base, std::nested_exception { };
29
30void test01()
31{
31483268 32 bool test = false;
ec206094 33
34 try
35 {
36 throw 42;
37 }
38 catch (...)
39 {
40 derived d;
41 try
42 {
43 std::rethrow_if_nested(d);
44 }
45 catch (const int& i)
46 {
47 test = true;
48 VERIFY( i == 42 );
49 }
50 }
51
52 VERIFY( test );
53}
54
55void test02()
56{
31483268 57 bool test = false;
ec206094 58
59 try
60 {
61 throw base();
62 }
63 catch (const base& b)
64 {
65 std::rethrow_if_nested(b);
66 test = true;
67 }
68
69 VERIFY( test );
70}
71
72void test03()
73{
31483268 74 bool test = false;
ec206094 75
76 try
77 {
78 throw 42;
79 }
80 catch (...)
81 {
82 try
83 {
84 throw derived2();
85 }
86 catch (const base& b)
87 {
88 try
89 {
90 std::rethrow_if_nested(b);
91 }
92 catch (const int& i)
93 {
94 VERIFY( i == 42 );
95 test = true;
96 }
97 }
98 }
99
100 VERIFY( test );
101}
102
06046b8d 103void
104test04()
105{
106 // LWG 2484 requires that these cases are well-formed, but don't rethrow.
107
108 std::rethrow_if_nested(1);
109
110 struct S { } nonpolymorphic;
111 std::rethrow_if_nested(nonpolymorphic);
112
113 struct derived3 : derived, derived2 { };
114 derived3 ambiguous_base;
115 std::rethrow_if_nested(ambiguous_base);
116
117 struct derived4 : private std::nested_exception { };
118 derived4 private_base;
119 std::rethrow_if_nested(private_base);
120}
ec206094 121
122int main()
123{
124 test01();
125 test02();
126 test03();
06046b8d 127 test04();
ec206094 128 return 0;
129}