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