jaulib v1.5.0
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
test_exceptions01.cpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2024 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24#include <cstring>
25#include <exception>
26#include <iostream>
27#include <stdexcept>
28#include <system_error>
29
30#include <jau/cpp_lang_util.hpp>
31#include <jau/test/catch2_ext.hpp>
32
33#include <jau/basic_types.hpp>
34#include <jau/exceptions.hpp>
36#include <jau/mp/big_int.hpp>
37
38using namespace jau;
39
40static void throwOutOfMemoryError() {
42}
43static void throwRuntimeException() {
45}
46static void throwLogicError() {
47 throw jau::LogicError("test", E_FILE_LINE);
48}
57}
59 throw jau::RuntimeSystemException(std::error_code(), "test", E_FILE_LINE);
60}
61static void throwIOError() {
62 throw jau::IOError("test", E_FILE_LINE);
63}
64static void throwInternalError() {
65 throw jau::InternalError("test", E_FILE_LINE);
66}
76
77TEST_CASE( "Exception 00", "[exceptions][error]" ) {
78 static_assert( true == std::is_base_of_v<jau::ExceptionBase, jau::OutOfMemoryError>);
79 static_assert( true == std::is_base_of_v<std::bad_alloc, jau::OutOfMemoryError>);
80 static_assert( true == std::is_base_of_v<std::exception, jau::OutOfMemoryError>);
81 REQUIRE_THROWS_MATCHES( throwOutOfMemoryError(), jau::OutOfMemoryError, Catch::Matchers::ContainsSubstring("OutOfMemoryError") );
82 REQUIRE_THROWS_AS( throwOutOfMemoryError(), jau::ExceptionBase);
83 REQUIRE_THROWS_AS( throwOutOfMemoryError(), std::bad_alloc);
84 REQUIRE_THROWS_AS( throwOutOfMemoryError(), std::exception);
85
86 static_assert( true == std::is_base_of_v<jau::ExceptionBase, jau::RuntimeExceptionBase>);
87 static_assert( true == std::is_base_of_v<jau::RuntimeExceptionBase, jau::RuntimeException>);
88 static_assert( true == std::is_base_of_v<std::runtime_error, jau::RuntimeException>);
89 static_assert( true == std::is_base_of_v<std::exception, jau::RuntimeException>);
90 REQUIRE_THROWS_MATCHES( throwRuntimeException(), jau::RuntimeException, Catch::Matchers::ContainsSubstring("RuntimeException") );
92 REQUIRE_THROWS_AS( throwRuntimeException(), std::runtime_error);
93 REQUIRE_THROWS_AS( throwRuntimeException(), std::exception);
94
95 static_assert( true == std::is_base_of_v<jau::ExceptionBase, jau::LogicErrorBase>);
96 static_assert( true == std::is_base_of_v<jau::LogicErrorBase, jau::LogicError>);
97 static_assert( true == std::is_base_of_v<std::logic_error, jau::LogicError>);
98 static_assert( true == std::is_base_of_v<std::exception, jau::LogicError>);
99 REQUIRE_THROWS_MATCHES( throwLogicError(), jau::LogicError, Catch::Matchers::ContainsSubstring("LogicError") );
100 REQUIRE_THROWS_AS( throwLogicError(), jau::LogicErrorBase);
101 REQUIRE_THROWS_AS( throwLogicError(), std::logic_error);
102 REQUIRE_THROWS_AS( throwLogicError(), std::exception);
103
104 static_assert( true == std::is_base_of_v<std::out_of_range, jau::IndexOutOfBoundsError>);
105 static_assert( true == std::is_base_of_v<jau::LogicErrorBase, jau::IndexOutOfBoundsError>);
106 static_assert( true == std::is_base_of_v<std::logic_error, jau::IndexOutOfBoundsError>);
107 REQUIRE_THROWS_MATCHES( throwIndexOutOfBoundsError(), jau::IndexOutOfBoundsError, Catch::Matchers::ContainsSubstring("IndexOutOfBoundsError") );
108 REQUIRE_THROWS_AS( throwIndexOutOfBoundsError(), std::out_of_range);
109 REQUIRE_THROWS_MATCHES( throwIndexOutOfBoundsError(), jau::LogicErrorBase, Catch::Matchers::ContainsSubstring("IndexOutOfBoundsError") );
110 REQUIRE_THROWS_AS( throwIndexOutOfBoundsError(), std::logic_error);
111 REQUIRE_THROWS_AS( throwIndexOutOfBoundsError(), std::exception);
112
113 static_assert( true == std::is_base_of_v<std::invalid_argument, jau::IllegalArgumentError>);
114 static_assert( true == std::is_base_of_v<jau::LogicErrorBase, jau::IllegalArgumentError>);
115 static_assert( true == std::is_base_of_v<std::logic_error, jau::IllegalArgumentError>);
116 REQUIRE_THROWS_MATCHES( throwIllegalArgumentError(), jau::IllegalArgumentError, Catch::Matchers::ContainsSubstring("IllegalArgumentError") );
117 REQUIRE_THROWS_AS( throwIllegalArgumentError(), std::invalid_argument);
118 REQUIRE_THROWS_MATCHES( throwIllegalArgumentError(), jau::LogicErrorBase, Catch::Matchers::ContainsSubstring("IllegalArgumentError") );
119 REQUIRE_THROWS_AS( throwIllegalArgumentError(), std::logic_error);
120 REQUIRE_THROWS_AS( throwIllegalArgumentError(), std::exception);
121
122 static_assert( true == std::is_base_of_v<std::domain_error, jau::IllegalStateError>);
123 static_assert( true == std::is_base_of_v<jau::LogicErrorBase, jau::IllegalStateError>);
124 static_assert( true == std::is_base_of_v<std::logic_error, jau::IllegalStateError>);
125 REQUIRE_THROWS_MATCHES( throwIllegalStateError(), jau::IllegalStateError, Catch::Matchers::ContainsSubstring("IllegalStateError") );
126 REQUIRE_THROWS_AS( throwIllegalStateError(), std::domain_error);
127 REQUIRE_THROWS_MATCHES( throwIllegalStateError(), jau::LogicErrorBase, Catch::Matchers::ContainsSubstring("IllegalStateError") );
128 REQUIRE_THROWS_AS( throwIllegalStateError(), std::logic_error);
129 REQUIRE_THROWS_AS( throwIllegalStateError(), std::exception);
130
131 static_assert( true == std::is_base_of_v<jau::RuntimeExceptionBase, jau::RuntimeSystemExceptionBase>);
132 static_assert( true == std::is_base_of_v<jau::ExceptionBase, jau::RuntimeSystemExceptionBase>);
133 static_assert( true == std::is_base_of_v<jau::RuntimeSystemExceptionBase, jau::RuntimeSystemException>);
134 static_assert( true == std::is_base_of_v<std::system_error, jau::RuntimeSystemException>);
135 static_assert( true == std::is_base_of_v<std::runtime_error, jau::RuntimeSystemException>);
136 static_assert( true == std::is_base_of_v<std::exception, jau::RuntimeSystemException>);
137 REQUIRE_THROWS_MATCHES( throwRuntimeSystemException(), jau::RuntimeSystemExceptionBase, Catch::Matchers::ContainsSubstring("RuntimeSystemException") );
138 REQUIRE_THROWS_AS( throwRuntimeSystemException(), std::system_error);
139 REQUIRE_THROWS_MATCHES( throwRuntimeSystemException(), jau::RuntimeExceptionBase, Catch::Matchers::ContainsSubstring("RuntimeSystemException") );
140 REQUIRE_THROWS_AS( throwRuntimeSystemException(), std::runtime_error);
141 REQUIRE_THROWS_AS( throwRuntimeSystemException(), std::exception);
142
143 static_assert( true == std::is_base_of_v<std::ios_base::failure, jau::IOError>);
144 static_assert( true == std::is_base_of_v<jau::RuntimeSystemExceptionBase, jau::IOError>);
145 static_assert( true == std::is_base_of_v<std::system_error, jau::IOError>);
146 static_assert( true == std::is_base_of_v<jau::RuntimeExceptionBase, jau::IOError>);
147 static_assert( true == std::is_base_of_v<std::runtime_error, jau::IOError>);
148 REQUIRE_THROWS_MATCHES( throwIOError(), jau::IOError, Catch::Matchers::ContainsSubstring("IOError") );
149 REQUIRE_THROWS_AS( throwIOError(), std::ios_base::failure );
150 REQUIRE_THROWS_MATCHES( throwIOError(), jau::RuntimeSystemExceptionBase, Catch::Matchers::ContainsSubstring("IOError") );
151 REQUIRE_THROWS_AS( throwIOError(), std::system_error);
152 REQUIRE_THROWS_MATCHES( throwIOError(), jau::RuntimeExceptionBase, Catch::Matchers::ContainsSubstring("IOError") );
153 REQUIRE_THROWS_AS( throwIOError(), std::runtime_error );
154 REQUIRE_THROWS_AS( throwIOError(), std::exception );
155
156 REQUIRE_THROWS_MATCHES( throwInternalError(), jau::RuntimeExceptionBase, Catch::Matchers::ContainsSubstring("InternalError") );
157 REQUIRE_THROWS_AS( throwInternalError(), std::runtime_error );
158 REQUIRE_THROWS_MATCHES( throwInternalError(), jau::InternalError, Catch::Matchers::ContainsSubstring("InternalError") );
159 REQUIRE_THROWS_AS( throwInternalError(), std::exception );
160
161 REQUIRE_THROWS_MATCHES( throwNotImplementedException(), jau::RuntimeExceptionBase, Catch::Matchers::ContainsSubstring("NotImplementedException") );
162 REQUIRE_THROWS_AS( throwNotImplementedException(), std::runtime_error );
163 REQUIRE_THROWS_MATCHES( throwNotImplementedException(), jau::NotImplementedException, Catch::Matchers::ContainsSubstring("NotImplementedException") );
164 REQUIRE_THROWS_AS( throwNotImplementedException(), std::exception );
165
166 REQUIRE_THROWS_MATCHES( throwNullPointerException(), jau::RuntimeExceptionBase, Catch::Matchers::ContainsSubstring("NullPointerException") );
167 REQUIRE_THROWS_AS( throwNullPointerException(), std::runtime_error );
168 REQUIRE_THROWS_MATCHES( throwNullPointerException(), jau::NullPointerException, Catch::Matchers::ContainsSubstring("NullPointerException") );
169 REQUIRE_THROWS_AS( throwNullPointerException(), std::exception );
170
171 REQUIRE_THROWS_MATCHES( throwUnsupportedOperationException(), jau::RuntimeExceptionBase, Catch::Matchers::ContainsSubstring("UnsupportedOperationException") );
172 REQUIRE_THROWS_AS( throwUnsupportedOperationException(), std::runtime_error );
173 REQUIRE_THROWS_MATCHES( throwUnsupportedOperationException(), jau::UnsupportedOperationException, Catch::Matchers::ContainsSubstring("UnsupportedOperationException") );
174 REQUIRE_THROWS_AS( throwUnsupportedOperationException(), std::exception );
175}
176
182}
183static void throwMathDomainError() {
185}
191}
195
196TEST_CASE( "Exception 10 Math", "[big_int_t][exceptions][error][math]" ) {
197 // MathError
198 static_assert( true == std::is_base_of_v<jau::math::MathErrorBase, jau::math::MathError>);
199 static_assert( true == std::is_base_of_v<std::exception, jau::math::MathError>);
200 REQUIRE_THROWS_MATCHES( throwMathError(), jau::math::MathErrorBase, Catch::Matchers::ContainsSubstring("MathError(undefined)") );
201 REQUIRE_THROWS_AS( throwMathError(), std::exception);
202
203 static_assert( true == std::is_base_of_v<jau::math::MathRuntimeErrorBase, jau::math::MathInexactError>);
204 static_assert( true == std::is_base_of_v<jau::math::MathErrorBase, jau::math::MathInexactError>);
205 static_assert( true == std::is_base_of_v<std::runtime_error, jau::math::MathInexactError>);
206 static_assert( true == std::is_base_of_v<std::exception, jau::math::MathInexactError>);
207 REQUIRE_THROWS_MATCHES( throwMathInexactError(), jau::math::MathInexactError, Catch::Matchers::ContainsSubstring("MathError(inexact)") );
209 REQUIRE_THROWS_AS( throwMathInexactError(), jau::math::MathErrorBase);
210 REQUIRE_THROWS_AS( throwMathInexactError(), std::runtime_error);
211 REQUIRE_THROWS_AS( throwMathInexactError(), std::exception);
212
213 static_assert( true == std::is_base_of_v<jau::math::MathRuntimeErrorBase, jau::math::MathOverflowError>);
214 static_assert( true == std::is_base_of_v<jau::math::MathErrorBase, jau::math::MathOverflowError>);
215 static_assert( true == std::is_base_of_v<std::overflow_error, jau::math::MathOverflowError>);
216 static_assert( true == std::is_base_of_v<std::runtime_error, jau::math::MathOverflowError>);
217 static_assert( true == std::is_base_of_v<std::exception, jau::math::MathOverflowError>);
218 REQUIRE_THROWS_MATCHES( throwMathOverflowError(), jau::math::MathOverflowError, Catch::Matchers::ContainsSubstring("MathError(overflow)") );
221 REQUIRE_THROWS_AS( throwMathOverflowError(), std::overflow_error);
222 REQUIRE_THROWS_AS( throwMathOverflowError(), std::runtime_error);
223 REQUIRE_THROWS_AS( throwMathOverflowError(), std::exception);
224
225 static_assert( true == std::is_base_of_v<jau::math::MathRuntimeErrorBase, jau::math::MathUnderflowError>);
226 static_assert( true == std::is_base_of_v<jau::math::MathErrorBase, jau::math::MathUnderflowError>);
227 static_assert( true == std::is_base_of_v<std::underflow_error, jau::math::MathUnderflowError>);
228 static_assert( true == std::is_base_of_v<std::runtime_error, jau::math::MathUnderflowError>);
229 static_assert( true == std::is_base_of_v<std::exception, jau::math::MathUnderflowError>);
230 REQUIRE_THROWS_MATCHES( throwMathUnderflowError(), jau::math::MathUnderflowError, Catch::Matchers::ContainsSubstring("MathError(underflow)") );
233 REQUIRE_THROWS_AS( throwMathUnderflowError(), std::underflow_error);
234 REQUIRE_THROWS_AS( throwMathUnderflowError(), std::runtime_error);
235 REQUIRE_THROWS_AS( throwMathUnderflowError(), std::exception);
236
237 static_assert( true == std::is_base_of_v<jau::math::MathErrorBase, jau::math::MathDomainError>);
238 static_assert( true == std::is_base_of_v<std::domain_error, jau::math::MathDomainError>);
239 static_assert( true == std::is_base_of_v<std::exception, jau::math::MathDomainError>);
240 REQUIRE_THROWS_MATCHES( throwMathDomainError(), jau::math::MathDomainError, Catch::Matchers::ContainsSubstring("MathError(invalid)") );
241 REQUIRE_THROWS_AS( throwMathDomainError(), jau::math::MathErrorBase);
242 REQUIRE_THROWS_AS( throwMathDomainError(), std::domain_error);
243 REQUIRE_THROWS_AS( throwMathDomainError(), std::exception);
244
245 static_assert( true == std::is_base_of_v<jau::math::MathErrorBase, jau::math::MathDivByZeroError>);
246 static_assert( true == std::is_base_of_v<jau::math::MathDomainError, jau::math::MathDivByZeroError>);
247 static_assert( true == std::is_base_of_v<std::domain_error, jau::math::MathDivByZeroError>);
248 static_assert( true == std::is_base_of_v<std::exception, jau::math::MathDivByZeroError>);
249 REQUIRE_THROWS_MATCHES( throwMathDivByZeroError(), jau::math::MathDivByZeroError, Catch::Matchers::ContainsSubstring("MathError(div_by_zero)") );
252 REQUIRE_THROWS_AS( throwMathDivByZeroError(), std::domain_error);
253 REQUIRE_THROWS_AS( throwMathDivByZeroError(), std::exception);
254 REQUIRE_THROWS_AS( throwMathDivByZeroError(), std::exception);
255}
256
257TEST_CASE( "Exception 11 Math", "[big_int_t][exceptions][error][arithmetic][math]" ) {
258 {
259 jau::mp::BigInt a = 1, b = 0, r;
260 REQUIRE_THROWS_MATCHES( r = a / b, jau::math::MathDivByZeroError, Catch::Matchers::ContainsSubstring("div_by_zero") );
261 REQUIRE_THROWS_MATCHES( r = a % b, jau::math::MathDivByZeroError, Catch::Matchers::ContainsSubstring("div_by_zero") );
262 }
263 {
265 REQUIRE_THROWS_MATCHES( r = a % b, jau::math::MathDomainError, Catch::Matchers::ContainsSubstring("invalid") );
266 }
267}
268
269TEST_CASE( "Exception 20 Catching", "[exceptions][error]" ) {
270 {
271 std::exception_ptr eptr;
272 try {
273 [[maybe_unused]]
274 char ch = std::string().at(1); // this generates a std::out_of_range
275 } catch (...) {
276 eptr = std::current_exception(); // capture
277 }
278
279 REQUIRE(true == jau::handle_exception(eptr, E_FILE_LINE));
280 }
281 {
282 jau::exception_handler_t eh = [](const std::exception &e, const char* file, int line) -> bool {
283 std::cerr << "Exception 20 @ " << file << ":" << line << ": " << e.what() << "\n";
284 return true;
285 };
286 std::exception_ptr eptr;
287
288 try {
289 [[maybe_unused]]
290 char ch = std::string().at(1); // this generates a std::out_of_range
291 } catch (...) {
292 eptr = std::current_exception(); // capture
293 }
294
295 REQUIRE(true == jau::handle_exception(eptr, eh, E_FILE_LINE));
296 }
297}
math_error_t::div_by_zero, i.e.
math_error_t::invalid
math_error_t::inexact
math_error_t::overflow
math_error_t::underflow
Arbitrary precision integer type.
Definition big_int.hpp:50
static BigInt from_s32(int32_t n)
Create big_int from a signed 32 bit integer.
Definition big_int.hpp:104
#define E_FILE_LINE
jau::function< bool(const std::exception &, const char *file, int line)> exception_handler_t
Exception handler method, should return true if not consumed or deemed an error. Shall be noexcept it...
bool handle_exception(std::exception_ptr eptr, const char *file, int line) noexcept
Handle given optional exception (nullable std::exception_ptr) and send std::exception::what() message...
@ undefined
undefined math error
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition backtrace.hpp:32
TEST_CASE("Exception 00", "[exceptions][error]")
static void throwIllegalArgumentError()
static void throwMathError()
static void throwLogicError()
static void throwMathUnderflowError()
static void throwUnsupportedOperationException()
static void throwOutOfMemoryError()
static void throwMathDivByZeroError()
static void throwRuntimeSystemException()
static void throwRuntimeException()
static void throwMathDomainError()
static void throwNotImplementedException()
static void throwIndexOutOfBoundsError()
static void throwMathInexactError()
static void throwIllegalStateError()
static void throwInternalError()
static void throwNullPointerException()
static void throwIOError()
static void throwMathOverflowError()