| 1 | // 2011-03-16 Benjamin Kosnik <[email protected]> |
| 2 | |
| 3 | // Copyright (C) 2011-2025 Free Software Foundation, Inc. |
| 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 <string> |
| 21 | #include <stdexcept> |
| 22 | #include <testsuite_hooks.h> |
| 23 | |
| 24 | void test01() |
| 25 | { |
| 26 | using namespace std; |
| 27 | std::string s("error"); |
| 28 | |
| 29 | try |
| 30 | { |
| 31 | logic_error e1(s); |
| 32 | throw e1; |
| 33 | } |
| 34 | catch(const exception& e) |
| 35 | { |
| 36 | s = e.what(); |
| 37 | } |
| 38 | |
| 39 | try |
| 40 | { |
| 41 | domain_error e2(s); |
| 42 | throw e2; |
| 43 | } |
| 44 | catch(const exception& e) |
| 45 | { |
| 46 | s = e.what(); |
| 47 | } |
| 48 | |
| 49 | try |
| 50 | { |
| 51 | invalid_argument e3(s); |
| 52 | throw e3; |
| 53 | } |
| 54 | catch(const exception& e) |
| 55 | { |
| 56 | s = e.what(); |
| 57 | } |
| 58 | |
| 59 | try |
| 60 | { |
| 61 | length_error e4(s); |
| 62 | throw e4; |
| 63 | } |
| 64 | catch(const exception& e) |
| 65 | { |
| 66 | s = e.what(); |
| 67 | } |
| 68 | |
| 69 | try |
| 70 | { |
| 71 | out_of_range e5(s); |
| 72 | throw e5; |
| 73 | } |
| 74 | catch(const exception& e) |
| 75 | { |
| 76 | s = e.what(); |
| 77 | } |
| 78 | |
| 79 | try |
| 80 | { |
| 81 | runtime_error e6(s); |
| 82 | throw e6; |
| 83 | } |
| 84 | catch(const exception& e) |
| 85 | { |
| 86 | s = e.what(); |
| 87 | } |
| 88 | |
| 89 | try |
| 90 | { |
| 91 | range_error e7(s); |
| 92 | throw e7; |
| 93 | } |
| 94 | catch(const exception& e) |
| 95 | { |
| 96 | s = e.what(); |
| 97 | } |
| 98 | |
| 99 | try |
| 100 | { |
| 101 | overflow_error e8(s); |
| 102 | throw e8; |
| 103 | } |
| 104 | catch(const exception& e) |
| 105 | { |
| 106 | s = e.what(); |
| 107 | } |
| 108 | |
| 109 | try |
| 110 | { |
| 111 | underflow_error e9(s); |
| 112 | throw e9; |
| 113 | } |
| 114 | catch(const exception& e) |
| 115 | { |
| 116 | s = e.what(); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | template<typename _Tp> |
| 121 | struct extra_error : public _Tp |
| 122 | { |
| 123 | extra_error(const std::string& s) : _Tp(s) { } |
| 124 | }; |
| 125 | |
| 126 | void test02() |
| 127 | { |
| 128 | using namespace std; |
| 129 | std::string s("error"); |
| 130 | |
| 131 | try |
| 132 | { |
| 133 | extra_error<logic_error> e1(s); |
| 134 | throw e1; |
| 135 | } |
| 136 | catch(const exception& e) |
| 137 | { |
| 138 | s = e.what(); |
| 139 | } |
| 140 | |
| 141 | try |
| 142 | { |
| 143 | extra_error<domain_error> e2(s); |
| 144 | throw e2; |
| 145 | } |
| 146 | catch(const exception& e) |
| 147 | { |
| 148 | s = e.what(); |
| 149 | } |
| 150 | |
| 151 | try |
| 152 | { |
| 153 | extra_error<invalid_argument> e3(s); |
| 154 | throw e3; |
| 155 | } |
| 156 | catch(const exception& e) |
| 157 | { |
| 158 | s = e.what(); |
| 159 | } |
| 160 | |
| 161 | try |
| 162 | { |
| 163 | extra_error<length_error> e4(s); |
| 164 | throw e4; |
| 165 | } |
| 166 | catch(const exception& e) |
| 167 | { |
| 168 | s = e.what(); |
| 169 | } |
| 170 | |
| 171 | try |
| 172 | { |
| 173 | extra_error<out_of_range> e5(s); |
| 174 | throw e5; |
| 175 | } |
| 176 | catch(const exception& e) |
| 177 | { |
| 178 | s = e.what(); |
| 179 | } |
| 180 | |
| 181 | try |
| 182 | { |
| 183 | extra_error<runtime_error> e6(s); |
| 184 | throw e6; |
| 185 | } |
| 186 | catch(const exception& e) |
| 187 | { |
| 188 | s = e.what(); |
| 189 | } |
| 190 | |
| 191 | try |
| 192 | { |
| 193 | extra_error<range_error> e7(s); |
| 194 | throw e7; |
| 195 | } |
| 196 | catch(const exception& e) |
| 197 | { |
| 198 | s = e.what(); |
| 199 | } |
| 200 | |
| 201 | try |
| 202 | { |
| 203 | extra_error<overflow_error> e8(s); |
| 204 | throw e8; |
| 205 | } |
| 206 | catch(const exception& e) |
| 207 | { |
| 208 | s = e.what(); |
| 209 | } |
| 210 | |
| 211 | try |
| 212 | { |
| 213 | extra_error<underflow_error> e9(s); |
| 214 | throw e9; |
| 215 | } |
| 216 | catch(const exception& e) |
| 217 | { |
| 218 | s = e.what(); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | void test03() |
| 223 | { |
| 224 | std::logic_error le1(""); |
| 225 | // Copy constructor: |
| 226 | std::logic_error le2(le1); |
| 227 | // Copy assignment operator: |
| 228 | le1 = le2; |
| 229 | #if __cplusplus >= 201103L |
| 230 | // Move constructor: |
| 231 | std::logic_error le3 = std::move(le1); |
| 232 | // Move assignment operator: |
| 233 | le1 = std::move(le3); |
| 234 | #endif |
| 235 | |
| 236 | std::runtime_error re1(""); |
| 237 | // Copy constructor: |
| 238 | std::runtime_error re2(re1); |
| 239 | // Copy assignment operator: |
| 240 | re1 = re2; |
| 241 | #if __cplusplus >= 201103L |
| 242 | // Move constructor: |
| 243 | std::runtime_error re3 = std::move(re1); |
| 244 | // Move assignment operator: |
| 245 | re1 = std::move(re3); |
| 246 | #endif |
| 247 | } |
| 248 | |
| 249 | int main(void) |
| 250 | { |
| 251 | test01(); |
| 252 | test02(); |
| 253 | test03(); |
| 254 | return 0; |
| 255 | } |