| 1 | // { dg-do run { target c++11 } } |
| 2 | |
| 3 | // 2008-05-25 Sebastian Redl <[email protected]> |
| 4 | |
| 5 | // Copyright (C) 2008-2025 Free Software Foundation, Inc. |
| 6 | // |
| 7 | // This file is part of the GNU ISO C++ Library. This library is free |
| 8 | // software; you can redistribute it and/or modify it under the |
| 9 | // terms of the GNU General Public License as published by the |
| 10 | // Free Software Foundation; either version 3, or (at your option) |
| 11 | // any later version. |
| 12 | |
| 13 | // This library is distributed in the hope that it will be useful, |
| 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | // GNU General Public License for more details. |
| 17 | |
| 18 | // You should have received a copy of the GNU General Public License along |
| 19 | // with this library; see the file COPYING3. If not see |
| 20 | // <http://www.gnu.org/licenses/>. |
| 21 | |
| 22 | // rethrow_exception() and preservation of data |
| 23 | |
| 24 | #include <exception> |
| 25 | #include <typeinfo> |
| 26 | #include <cstring> |
| 27 | #include <stdexcept> |
| 28 | #include <testsuite_hooks.h> |
| 29 | |
| 30 | void test01() |
| 31 | { |
| 32 | using namespace std; |
| 33 | |
| 34 | try { |
| 35 | rethrow_exception(make_exception_ptr(0)); |
| 36 | } catch(...) { |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | void test02() |
| 41 | { |
| 42 | using namespace std; |
| 43 | |
| 44 | try { |
| 45 | rethrow_exception(make_exception_ptr(runtime_error("test"))); |
| 46 | } catch(exception &e) { |
| 47 | #if __cpp_rtti |
| 48 | VERIFY( typeid(e) == typeid(runtime_error) ); |
| 49 | #endif |
| 50 | VERIFY( strcmp(e.what(), "test") == 0 ); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void test03() |
| 55 | { |
| 56 | using namespace std; |
| 57 | |
| 58 | exception_ptr ep; |
| 59 | try { |
| 60 | throw 0; |
| 61 | } catch(...) { |
| 62 | ep = current_exception(); |
| 63 | } |
| 64 | try { |
| 65 | rethrow_exception(ep); |
| 66 | } catch(...) { |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void test04() |
| 71 | { |
| 72 | using namespace std; |
| 73 | |
| 74 | // Weave the exceptions in an attempt to confuse the machinery. |
| 75 | try { |
| 76 | throw 0; |
| 77 | } catch(...) { |
| 78 | exception_ptr ep1 = current_exception(); |
| 79 | try { |
| 80 | throw 1; |
| 81 | } catch(...) { |
| 82 | exception_ptr ep2 = current_exception(); |
| 83 | try { |
| 84 | rethrow_exception(ep1); |
| 85 | } catch(...) { |
| 86 | try { |
| 87 | rethrow_exception(ep2); |
| 88 | } catch(...) { |
| 89 | try { |
| 90 | rethrow_exception(ep1); |
| 91 | } catch(...) { |
| 92 | } |
| 93 | try { |
| 94 | rethrow_exception(ep2); |
| 95 | } catch(...) { |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void test05() |
| 104 | { |
| 105 | // libstdc++/64651 std::rethrow_exception not found by ADL |
| 106 | // This is not required to work but is a conforming extension. |
| 107 | try { |
| 108 | rethrow_exception(std::make_exception_ptr(0)); |
| 109 | } catch(...) { |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | int main() |
| 114 | { |
| 115 | test01(); |
| 116 | test02(); |
| 117 | test03(); |
| 118 | test04(); |
| 119 | test05(); |
| 120 | |
| 121 | return 0; |
| 122 | } |