jaulib v1.4.1-17-gd77ace3-dirty
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/test/catch2_ext.hpp>
31
32#include <jau/basic_types.hpp>
33#include <jau/exceptions.hpp>
35#include <jau/mp/big_int.hpp>
36
37using namespace jau;
38
39static void throwOutOfMemoryError() {
41}
42static void throwRuntimeException() {
44}
45static void throwLogicError() {
46 throw jau::LogicError("test", E_FILE_LINE);
47}
56}
58 throw jau::RuntimeSystemException(std::error_code(), "test", E_FILE_LINE);
59}
60static void throwIOError() {
61 throw jau::IOError("test", E_FILE_LINE);
62}
63static void throwInternalError() {
64 throw jau::InternalError("test", E_FILE_LINE);
65}
75
76TEST_CASE( "Exception 00", "[exceptions][error]" ) {
77 static_assert( true == std::is_base_of_v<jau::ExceptionBase, jau::OutOfMemoryError>);
78 static_assert( true == std::is_base_of_v<std::bad_alloc, jau::OutOfMemoryError>);
79 static_assert( true == std::is_base_of_v<std::exception, jau::OutOfMemoryError>);
80 REQUIRE_THROWS_MATCHES( throwOutOfMemoryError(), jau::OutOfMemoryError, Catch::Matchers::ContainsSubstring("OutOfMemoryError") );
81 REQUIRE_THROWS_AS( throwOutOfMemoryError(), jau::ExceptionBase);
82 REQUIRE_THROWS_AS( throwOutOfMemoryError(), std::bad_alloc);
83 REQUIRE_THROWS_AS( throwOutOfMemoryError(), std::exception);
84
85 static_assert( true == std::is_base_of_v<jau::ExceptionBase, jau::RuntimeExceptionBase>);
86 static_assert( true == std::is_base_of_v<jau::RuntimeExceptionBase, jau::RuntimeException>);
87 static_assert( true == std::is_base_of_v<std::runtime_error, jau::RuntimeException>);
88 static_assert( true == std::is_base_of_v<std::exception, jau::RuntimeException>);
89 REQUIRE_THROWS_MATCHES( throwRuntimeException(), jau::RuntimeException, Catch::Matchers::ContainsSubstring("RuntimeException") );
91 REQUIRE_THROWS_AS( throwRuntimeException(), std::runtime_error);
92 REQUIRE_THROWS_AS( throwRuntimeException(), std::exception);
93
94 static_assert( true == std::is_base_of_v<jau::ExceptionBase, jau::LogicErrorBase>);
95 static_assert( true == std::is_base_of_v<jau::LogicErrorBase, jau::LogicError>);
96 static_assert( true == std::is_base_of_v<std::logic_error, jau::LogicError>);
97 static_assert( true == std::is_base_of_v<std::exception, jau::LogicError>);
98 REQUIRE_THROWS_MATCHES( throwLogicError(), jau::LogicError, Catch::Matchers::ContainsSubstring("LogicError") );
99 REQUIRE_THROWS_AS( throwLogicError(), jau::LogicErrorBase);
100 REQUIRE_THROWS_AS( throwLogicError(), std::logic_error);
101 REQUIRE_THROWS_AS( throwLogicError(), std::exception);
102
103 static_assert( true == std::is_base_of_v<std::out_of_range, jau::IndexOutOfBoundsError>);
104 static_assert( true == std::is_base_of_v<jau::LogicErrorBase, jau::IndexOutOfBoundsError>);
105 static_assert( true == std::is_base_of_v<std::logic_error, jau::IndexOutOfBoundsError>);
106 REQUIRE_THROWS_MATCHES( throwIndexOutOfBoundsError(), jau::IndexOutOfBoundsError, Catch::Matchers::ContainsSubstring("IndexOutOfBoundsError") );
107 REQUIRE_THROWS_AS( throwIndexOutOfBoundsError(), std::out_of_range);
108 REQUIRE_THROWS_MATCHES( throwIndexOutOfBoundsError(), jau::LogicErrorBase, Catch::Matchers::ContainsSubstring("IndexOutOfBoundsError") );
109 REQUIRE_THROWS_AS( throwIndexOutOfBoundsError(), std::logic_error);
110 REQUIRE_THROWS_AS( throwIndexOutOfBoundsError(), std::exception);
111
112 static_assert( true == std::is_base_of_v<std::invalid_argument, jau::IllegalArgumentError>);
113 static_assert( true == std::is_base_of_v<jau::LogicErrorBase, jau::IllegalArgumentError>);
114 static_assert( true == std::is_base_of_v<std::logic_error, jau::IllegalArgumentError>);
115 REQUIRE_THROWS_MATCHES( throwIllegalArgumentError(), jau::IllegalArgumentError, Catch::Matchers::ContainsSubstring("IllegalArgumentError") );
116 REQUIRE_THROWS_AS( throwIllegalArgumentError(), std::invalid_argument);
117 REQUIRE_THROWS_MATCHES( throwIllegalArgumentError(), jau::LogicErrorBase, Catch::Matchers::ContainsSubstring("IllegalArgumentError") );
118 REQUIRE_THROWS_AS( throwIllegalArgumentError(), std::logic_error);
119 REQUIRE_THROWS_AS( throwIllegalArgumentError(), std::exception);
120
121 static_assert( true == std::is_base_of_v<std::domain_error, jau::IllegalStateError>);
122 static_assert( true == std::is_base_of_v<jau::LogicErrorBase, jau::IllegalStateError>);
123 static_assert( true == std::is_base_of_v<std::logic_error, jau::IllegalStateError>);
124 REQUIRE_THROWS_MATCHES( throwIllegalStateError(), jau::IllegalStateError, Catch::Matchers::ContainsSubstring("IllegalStateError") );
125 REQUIRE_THROWS_AS( throwIllegalStateError(), std::domain_error);
126 REQUIRE_THROWS_MATCHES( throwIllegalStateError(), jau::LogicErrorBase, Catch::Matchers::ContainsSubstring("IllegalStateError") );
127 REQUIRE_THROWS_AS( throwIllegalStateError(), std::logic_error);
128 REQUIRE_THROWS_AS( throwIllegalStateError(), std::exception);
129
130 static_assert( true == std::is_base_of_v<jau::RuntimeExceptionBase, jau::RuntimeSystemExceptionBase>);
131 static_assert( true == std::is_base_of_v<jau::ExceptionBase, jau::RuntimeSystemExceptionBase>);
132 static_assert( true == std::is_base_of_v<jau::RuntimeSystemExceptionBase, jau::RuntimeSystemException>);
133 static_assert( true == std::is_base_of_v<std::system_error, jau::RuntimeSystemException>);
134 static_assert( true == std::is_base_of_v<std::runtime_error, jau::RuntimeSystemException>);
135 static_assert( true == std::is_base_of_v<std::exception, jau::RuntimeSystemException>);
136 REQUIRE_THROWS_MATCHES( throwRuntimeSystemException(), jau::RuntimeSystemExceptionBase, Catch::Matchers::ContainsSubstring("RuntimeSystemException") );
137 REQUIRE_THROWS_AS( throwRuntimeSystemException(), std::system_error);
138 REQUIRE_THROWS_MATCHES( throwRuntimeSystemException(), jau::RuntimeExceptionBase, Catch::Matchers::ContainsSubstring("RuntimeSystemException") );
139 REQUIRE_THROWS_AS( throwRuntimeSystemException(), std::runtime_error);
140 REQUIRE_THROWS_AS( throwRuntimeSystemException(), std::exception);
141
142 static_assert( true == std::is_base_of_v<std::ios_base::failure, jau::IOError>);
143 static_assert( true == std::is_base_of_v<jau::RuntimeSystemExceptionBase, jau::IOError>);
144 static_assert( true == std::is_base_of_v<std::system_error, jau::IOError>);
145 static_assert( true == std::is_base_of_v<jau::RuntimeExceptionBase, jau::IOError>);
146 static_assert( true == std::is_base_of_v<std::runtime_error, jau::IOError>);
147 REQUIRE_THROWS_MATCHES( throwIOError(), jau::IOError, Catch::Matchers::ContainsSubstring("IOError") );
148 REQUIRE_THROWS_AS( throwIOError(), std::ios_base::failure );
149 REQUIRE_THROWS_MATCHES( throwIOError(), jau::RuntimeSystemExceptionBase, Catch::Matchers::ContainsSubstring("IOError") );
150 REQUIRE_THROWS_AS( throwIOError(), std::system_error);
151 REQUIRE_THROWS_MATCHES( throwIOError(), jau::RuntimeExceptionBase, Catch::Matchers::ContainsSubstring("IOError") );
152 REQUIRE_THROWS_AS( throwIOError(), std::runtime_error );
153 REQUIRE_THROWS_AS( throwIOError(), std::exception );
154
155 REQUIRE_THROWS_MATCHES( throwInternalError(), jau::RuntimeExceptionBase, Catch::Matchers::ContainsSubstring("InternalError") );
156 REQUIRE_THROWS_AS( throwInternalError(), std::runtime_error );
157 REQUIRE_THROWS_MATCHES( throwInternalError(), jau::InternalError, Catch::Matchers::ContainsSubstring("InternalError") );
158 REQUIRE_THROWS_AS( throwInternalError(), std::exception );
159
160 REQUIRE_THROWS_MATCHES( throwNotImplementedException(), jau::RuntimeExceptionBase, Catch::Matchers::ContainsSubstring("NotImplementedException") );
161 REQUIRE_THROWS_AS( throwNotImplementedException(), std::runtime_error );
162 REQUIRE_THROWS_MATCHES( throwNotImplementedException(), jau::NotImplementedException, Catch::Matchers::ContainsSubstring("NotImplementedException") );
163 REQUIRE_THROWS_AS( throwNotImplementedException(), std::exception );
164
165 REQUIRE_THROWS_MATCHES( throwNullPointerException(), jau::RuntimeExceptionBase, Catch::Matchers::ContainsSubstring("NullPointerException") );
166 REQUIRE_THROWS_AS( throwNullPointerException(), std::runtime_error );
167 REQUIRE_THROWS_MATCHES( throwNullPointerException(), jau::NullPointerException, Catch::Matchers::ContainsSubstring("NullPointerException") );
168 REQUIRE_THROWS_AS( throwNullPointerException(), std::exception );
169
170 REQUIRE_THROWS_MATCHES( throwUnsupportedOperationException(), jau::RuntimeExceptionBase, Catch::Matchers::ContainsSubstring("UnsupportedOperationException") );
171 REQUIRE_THROWS_AS( throwUnsupportedOperationException(), std::runtime_error );
172 REQUIRE_THROWS_MATCHES( throwUnsupportedOperationException(), jau::UnsupportedOperationException, Catch::Matchers::ContainsSubstring("UnsupportedOperationException") );
173 REQUIRE_THROWS_AS( throwUnsupportedOperationException(), std::exception );
174}
175
181}
182static void throwMathDomainError() {
184}
190}
194
195TEST_CASE( "Exception 10 Math", "[big_int_t][exceptions][error][math]" ) {
196 // MathError
197 static_assert( true == std::is_base_of_v<jau::math::MathErrorBase, jau::math::MathError>);
198 static_assert( true == std::is_base_of_v<std::exception, jau::math::MathError>);
199 REQUIRE_THROWS_MATCHES( throwMathError(), jau::math::MathErrorBase, Catch::Matchers::ContainsSubstring("MathError(undefined)") );
200 REQUIRE_THROWS_AS( throwMathError(), std::exception);
201
202 static_assert( true == std::is_base_of_v<jau::math::MathRuntimeErrorBase, jau::math::MathInexactError>);
203 static_assert( true == std::is_base_of_v<jau::math::MathErrorBase, jau::math::MathInexactError>);
204 static_assert( true == std::is_base_of_v<std::runtime_error, jau::math::MathInexactError>);
205 static_assert( true == std::is_base_of_v<std::exception, jau::math::MathInexactError>);
206 REQUIRE_THROWS_MATCHES( throwMathInexactError(), jau::math::MathInexactError, Catch::Matchers::ContainsSubstring("MathError(inexact)") );
208 REQUIRE_THROWS_AS( throwMathInexactError(), jau::math::MathErrorBase);
209 REQUIRE_THROWS_AS( throwMathInexactError(), std::runtime_error);
210 REQUIRE_THROWS_AS( throwMathInexactError(), std::exception);
211
212 static_assert( true == std::is_base_of_v<jau::math::MathRuntimeErrorBase, jau::math::MathOverflowError>);
213 static_assert( true == std::is_base_of_v<jau::math::MathErrorBase, jau::math::MathOverflowError>);
214 static_assert( true == std::is_base_of_v<std::overflow_error, jau::math::MathOverflowError>);
215 static_assert( true == std::is_base_of_v<std::runtime_error, jau::math::MathOverflowError>);
216 static_assert( true == std::is_base_of_v<std::exception, jau::math::MathOverflowError>);
217 REQUIRE_THROWS_MATCHES( throwMathOverflowError(), jau::math::MathOverflowError, Catch::Matchers::ContainsSubstring("MathError(overflow)") );
220 REQUIRE_THROWS_AS( throwMathOverflowError(), std::overflow_error);
221 REQUIRE_THROWS_AS( throwMathOverflowError(), std::runtime_error);
222 REQUIRE_THROWS_AS( throwMathOverflowError(), std::exception);
223
224 static_assert( true == std::is_base_of_v<jau::math::MathRuntimeErrorBase, jau::math::MathUnderflowError>);
225 static_assert( true == std::is_base_of_v<jau::math::MathErrorBase, jau::math::MathUnderflowError>);
226 static_assert( true == std::is_base_of_v<std::underflow_error, jau::math::MathUnderflowError>);
227 static_assert( true == std::is_base_of_v<std::runtime_error, jau::math::MathUnderflowError>);
228 static_assert( true == std::is_base_of_v<std::exception, jau::math::MathUnderflowError>);
229 REQUIRE_THROWS_MATCHES( throwMathUnderflowError(), jau::math::MathUnderflowError, Catch::Matchers::ContainsSubstring("MathError(underflow)") );
232 REQUIRE_THROWS_AS( throwMathUnderflowError(), std::underflow_error);
233 REQUIRE_THROWS_AS( throwMathUnderflowError(), std::runtime_error);
234 REQUIRE_THROWS_AS( throwMathUnderflowError(), std::exception);
235
236 static_assert( true == std::is_base_of_v<jau::math::MathErrorBase, jau::math::MathDomainError>);
237 static_assert( true == std::is_base_of_v<std::domain_error, jau::math::MathDomainError>);
238 static_assert( true == std::is_base_of_v<std::exception, jau::math::MathDomainError>);
239 REQUIRE_THROWS_MATCHES( throwMathDomainError(), jau::math::MathDomainError, Catch::Matchers::ContainsSubstring("MathError(invalid)") );
240 REQUIRE_THROWS_AS( throwMathDomainError(), jau::math::MathErrorBase);
241 REQUIRE_THROWS_AS( throwMathDomainError(), std::domain_error);
242 REQUIRE_THROWS_AS( throwMathDomainError(), std::exception);
243
244 static_assert( true == std::is_base_of_v<jau::math::MathErrorBase, jau::math::MathDivByZeroError>);
245 static_assert( true == std::is_base_of_v<jau::math::MathDomainError, jau::math::MathDivByZeroError>);
246 static_assert( true == std::is_base_of_v<std::domain_error, jau::math::MathDivByZeroError>);
247 static_assert( true == std::is_base_of_v<std::exception, jau::math::MathDivByZeroError>);
248 REQUIRE_THROWS_MATCHES( throwMathDivByZeroError(), jau::math::MathDivByZeroError, Catch::Matchers::ContainsSubstring("MathError(div_by_zero)") );
251 REQUIRE_THROWS_AS( throwMathDivByZeroError(), std::domain_error);
252 REQUIRE_THROWS_AS( throwMathDivByZeroError(), std::exception);
253 REQUIRE_THROWS_AS( throwMathDivByZeroError(), std::exception);
254}
255
256TEST_CASE( "Exception 11 Math", "[big_int_t][exceptions][error][arithmetic][math]" ) {
257 {
258 jau::mp::BigInt a = 1, b = 0, r;
259 REQUIRE_THROWS_MATCHES( r = a / b, jau::math::MathDivByZeroError, Catch::Matchers::ContainsSubstring("div_by_zero") );
260 REQUIRE_THROWS_MATCHES( r = a % b, jau::math::MathDivByZeroError, Catch::Matchers::ContainsSubstring("div_by_zero") );
261 }
262 {
264 REQUIRE_THROWS_MATCHES( r = a % b, jau::math::MathDomainError, Catch::Matchers::ContainsSubstring("invalid") );
265 }
266}
267
268TEST_CASE( "Exception 20 Catching", "[exceptions][error]" ) {
269 {
270 jau::exception_handler_t eh = [](const std::exception &e) -> bool {
271 std::cerr << "Exception 20: " << e.what() << "\n";
272 return true;
273 };
274 std::exception_ptr eptr;
275
276 try {
277 [[maybe_unused]]
278 char ch = std::string().at(1); // this generates a std::out_of_range
279 } catch (...) {
280 eptr = std::current_exception(); // capture
281 }
282
283 REQUIRE(true == jau::handle_exception(eptr, eh));
284 }
285}
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
bool handle_exception(std::exception_ptr eptr)
Handle given optional exception (nullable std::exception_ptr) and send std::exception::what() message...
jau::function< bool(const std::exception &)> exception_handler_t
@ 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()