|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Exception handling system with formatted messages for Aleph-w. More...
#include <stdexcept>#include <sstream>#include <type_traits>Go to the source code of this file.
Classes | |
| class | Aleph::error_detail::Message_Stream |
| The stream of the message of a macro that throws. More... | |
| struct | Aleph::ExceptionBuilder< E > |
| Exception constructor with formatted message. More... | |
Namespaces | |
| namespace | Aleph |
| Main namespace for Aleph-w library functions. | |
| namespace | Aleph::error_detail |
| What the macros that throw need besides ExceptionBuilder. | |
Macros | |
| #define | AH_ERROR_COLD |
| #define | AH_ERRORS_JOIN_(a, b) a ## b |
| #define | AH_ERRORS_JOIN(a, b) AH_ERRORS_JOIN_(a, b) |
| #define | AH_WARNING_ONCE AH_ERRORS_JOIN(ah_warning_once_, __LINE__) |
| #define | AH_ERROR_UNLIKELY [[unlikely]] |
| #define | ah_warning_unless(out, C) |
| Emits a warning to a stream if the condition does NOT hold. | |
| #define | ah_warning_if(out, C) |
| Emits a warning to a stream if the condition holds. | |
| #define | ah_warning(out) (out) << "WARNING (" << __FILE__ << ":" << __LINE__ << ") | " |
| Emits an unconditional warning to a stream. | |
| #define | ah_range_error_unless(C) |
| Throws std::range_error if condition does NOT hold. | |
| #define | ah_range_error_if(C) |
| Throws std::range_error if condition holds. | |
| #define | ah_range_error() |
| Throws std::range_error unconditionally. | |
| #define | ah_range_error_if_constexpr(C) |
| #define | ah_range_error_unless_constexpr(C) |
| #define | ah_runtime_error_unless(C) |
| Throws std::runtime_error if condition does NOT hold. | |
| #define | ah_runtime_error_if(C) |
| Throws std::runtime_error if condition holds. | |
| #define | ah_runtime_error() |
| Throws std::runtime_error unconditionally. | |
| #define | ah_runtime_error_if_constexpr(C) |
| #define | ah_runtime_error_unless_constexpr(C) |
| #define | ah_logic_error_unless(C) |
| Throws std::logic_error if condition does NOT hold. | |
| #define | ah_logic_error_if(C) |
| Throws std::logic_error if condition holds. | |
| #define | ah_logic_error() |
| Throws std::logic_error unconditionally. | |
| #define | ah_logic_error_if_constexpr(C) |
| #define | ah_logic_error_unless_constexpr(C) |
| #define | ah_underflow_error_if(C) |
| Throws std::underflow_error if condition holds. | |
| #define | ah_underflow_error_unless(C) |
| Throws std::underflow_error if condition does NOT hold. | |
| #define | ah_underflow_error() |
| Throws std::underflow_error unconditionally. | |
| #define | ah_underflow_error_if_constexpr(C) |
| #define | ah_underflow_error_unless_constexpr(C) |
| #define | ah_bad_alloc() do { [[unlikely]] throw std::bad_alloc(); } while (false) |
| Throws std::bad_alloc unconditionally (no message stream) | |
| #define | ah_bad_alloc_if(C) |
| Throws std::bad_alloc if condition holds. | |
| #define | ah_bad_alloc_unless(C) do { if (!(C)) [[unlikely]] throw std::bad_alloc(); } while (false) |
| Throws std::bad_alloc if condition does NOT hold. | |
| #define | ah_bad_alloc_if_constexpr(C) do { if constexpr (C) throw std::bad_alloc(); } while (false) |
| #define | ah_bad_alloc_unless_constexpr(C) do { if constexpr (not (C)) throw std::bad_alloc(); } while (false) |
| #define | ah_overflow_error_if(C) |
| Throws std::overflow_error if condition holds. | |
| #define | ah_overflow_error_unless(C) |
| Throws std::overflow_error if condition does NOT hold. | |
| #define | ah_overflow_error() |
| Throws std::overflow_error unconditionally. | |
| #define | ah_overflow_error_if_constexpr(C) |
| #define | ah_overflow_error_unless_constexpr(C) |
| #define | ah_domain_error_if(C) |
| Throws std::domain_error if condition holds. | |
| #define | ah_domain_error_unless(C) |
| Throws std::domain_error if condition does NOT hold. | |
| #define | ah_domain_error() |
| Throws std::domain_error unconditionally. | |
| #define | ah_domain_error_if_constexpr(C) |
| #define | ah_domain_error_unless_constexpr(C) |
| #define | ah_out_of_range_error_if(C) |
| Throws std::out_of_range if condition holds. | |
| #define | ah_out_of_range_error_unless(C) |
| Throws std::out_of_range if condition does NOT hold. | |
| #define | ah_out_of_range_error() |
| Throws std::out_of_range unconditionally. | |
| #define | ah_out_of_range_error_if_constexpr(C) |
| #define | ah_out_of_range_error_unless_constexpr(C) |
| #define | ah_invalid_argument_if(C) |
| Throws std::invalid_argument if condition holds. | |
| #define | ah_invalid_argument_unless(C) |
| Throws std::invalid_argument if condition does NOT hold. | |
| #define | ah_invalid_argument() |
| Throws std::invalid_argument unconditionally. | |
| #define | ah_invalid_argument_if_constexpr(C) |
| #define | ah_invalid_argument_unless_constexpr(C) |
| #define | ah_length_error_if(C) |
| Throws std::length_error if condition holds. | |
| #define | ah_length_error_unless(C) |
| Throws std::length_error if condition does NOT hold. | |
| #define | ah_length_error() |
| Throws std::length_error unconditionally. | |
| #define | ah_length_error_if_constexpr(C) |
| #define | ah_length_error_unless_constexpr(C) |
| #define | ah_fatal_error() |
| Throws an unconditional std::runtime_error (fatal error) | |
Exception handling system with formatted messages for Aleph-w.
This file provides a complete set of macros to throw standard C++ exceptions with custom messages and automatic code location tracking.
Every _if and _unless macro, their _constexpr forms included, expands to a loop whose statement runs at most once: a while whose statement throws, a for of one iteration for the warnings, and do { ... } while (false) for the forms of ah_bad_alloc. A loop takes no else, so an else written after an invocation belongs to the if around it, and one with no if around it does not compile:
The condition is evaluated once, and it converts to bool as that of an if does, so a scoped enumeration does not; it must be an expression, not a declaration as in if (auto e = f()). The _constexpr forms require a constant condition, and their message is compiled even where the condition is false, so it must compile for every instantiation. A message whose last << does not give back a stream does not compile.
A macro that throws writes its message in a std::ostringstream, with the insertions of its caller, as it always did. That stream does not live in the frame of the function that checks: an object of one pointer, error_detail::Message_Stream, makes it when the macro throws and frees it when the exception leaves. A stream in that frame, 376 bytes with libstdc++ on x86-64, kept GCC from inlining a checked access as Array::operator[], and each access was a call.
The message does not change. Its items go into a stream of the same type, in their order and when they are evaluated, and only if the macro throws. Out of the message, two things do change:
std::bad_alloc in the place of its exception, and does not evaluate its items.(file:line) |, is written by the library, with the inserters of the standard library. An overload of << for an rvalue std::ostringstream that is visible where a macro is used takes the items of that use, and no longer the place.Definition in file ah-errors.H.
| #define ah_bad_alloc | ( | ) | do { [[unlikely]] throw std::bad_alloc(); } while (false) |
Throws std::bad_alloc unconditionally (no message stream)
Definition at line 570 of file ah-errors.H.
| #define ah_bad_alloc_if | ( | C | ) |
Throws std::bad_alloc if condition holds.
Definition at line 578 of file ah-errors.H.
| #define ah_bad_alloc_if_constexpr | ( | C | ) | do { if constexpr (C) throw std::bad_alloc(); } while (false) |
Definition at line 590 of file ah-errors.H.
| #define ah_bad_alloc_unless | ( | C | ) | do { if (!(C)) [[unlikely]] throw std::bad_alloc(); } while (false) |
Throws std::bad_alloc if condition does NOT hold.
Definition at line 587 of file ah-errors.H.
| #define ah_bad_alloc_unless_constexpr | ( | C | ) | do { if constexpr (not (C)) throw std::bad_alloc(); } while (false) |
Definition at line 593 of file ah-errors.H.
| #define ah_domain_error | ( | ) |
Throws std::domain_error unconditionally.
| std::domain_error | always |
Throws a domain_error without evaluating any condition. Use when you need to throw directly.
*
Definition at line 702 of file ah-errors.H.
| #define ah_domain_error_if | ( | C | ) |
Throws std::domain_error if condition holds.
| C | Condition to evaluate |
| std::domain_error | if C is true |
Useful when a value is outside the valid domain of a function.
*
Definition at line 670 of file ah-errors.H.
| #define ah_domain_error_if_constexpr | ( | C | ) |
Definition at line 705 of file ah-errors.H.
| #define ah_domain_error_unless | ( | C | ) |
Throws std::domain_error if condition does NOT hold.
| C | Condition to evaluate |
| std::domain_error | if C is false |
Useful for validating that a value is in the valid domain.
*
Definition at line 686 of file ah-errors.H.
| #define ah_domain_error_unless_constexpr | ( | C | ) |
Definition at line 708 of file ah-errors.H.
| #define AH_ERROR_COLD |
Definition at line 142 of file ah-errors.H.
| #define AH_ERROR_UNLIKELY [[unlikely]] |
Definition at line 273 of file ah-errors.H.
| #define AH_ERRORS_JOIN | ( | a, | |
| b | |||
| ) | AH_ERRORS_JOIN_(a, b) |
Definition at line 264 of file ah-errors.H.
| #define AH_ERRORS_JOIN_ | ( | a, | |
| b | |||
| ) | a ## b |
Definition at line 263 of file ah-errors.H.
| #define ah_fatal_error | ( | ) |
Throws an unconditional std::runtime_error (fatal error)
| std::runtime_error | always |
Useful for situations that should never occur or unrecoverable errors. Does not require a condition.
*
Definition at line 907 of file ah-errors.H.
| #define ah_invalid_argument | ( | ) |
Throws std::invalid_argument unconditionally.
| std::invalid_argument | always |
Throws an invalid_argument error without evaluating any condition. Use when you need to throw directly.
*
Definition at line 819 of file ah-errors.H.
| #define ah_invalid_argument_if | ( | C | ) |
Throws std::invalid_argument if condition holds.
| C | Condition to evaluate |
| std::invalid_argument | if C is true |
Useful for validating function arguments. Most common exception for incorrect parameter validation.
*
Definition at line 787 of file ah-errors.H.
| #define ah_invalid_argument_if_constexpr | ( | C | ) |
Definition at line 823 of file ah-errors.H.
| #define ah_invalid_argument_unless | ( | C | ) |
Throws std::invalid_argument if condition does NOT hold.
| C | Condition to evaluate |
| std::invalid_argument | if C is false |
Useful for validating that arguments meet required conditions.
*
Definition at line 803 of file ah-errors.H.
| #define ah_invalid_argument_unless_constexpr | ( | C | ) |
Definition at line 827 of file ah-errors.H.
| #define ah_length_error | ( | ) |
Throws std::length_error unconditionally.
| std::length_error | always |
Throws a length_error without evaluating any condition. Use when you need to throw directly.
*
Definition at line 878 of file ah-errors.H.
| #define ah_length_error_if | ( | C | ) |
Throws std::length_error if condition holds.
| C | Condition to evaluate |
| std::length_error | if C is true |
Useful when an operation exceeds container size limits.
*
Definition at line 846 of file ah-errors.H.
| #define ah_length_error_if_constexpr | ( | C | ) |
Definition at line 882 of file ah-errors.H.
| #define ah_length_error_unless | ( | C | ) |
Throws std::length_error if condition does NOT hold.
| C | Condition to evaluate |
| std::length_error | if C is false |
Useful for validating that size is within allowed limits.
*
Definition at line 862 of file ah-errors.H.
| #define ah_length_error_unless_constexpr | ( | C | ) |
Definition at line 886 of file ah-errors.H.
| #define ah_logic_error | ( | ) |
Throws std::logic_error unconditionally.
| std::logic_error | always |
Throws a logic_error without evaluating any condition. Use when you need to throw directly.
*
Definition at line 490 of file ah-errors.H.
| #define ah_logic_error_if | ( | C | ) |
Throws std::logic_error if condition holds.
| C | Condition to evaluate |
| std::logic_error | if C is true |
Useful for detecting program logic errors.
*
Definition at line 474 of file ah-errors.H.
| #define ah_logic_error_if_constexpr | ( | C | ) |
Definition at line 494 of file ah-errors.H.
| #define ah_logic_error_unless | ( | C | ) |
Throws std::logic_error if condition does NOT hold.
| C | Condition to evaluate |
| std::logic_error | if C is false |
Useful for program logic errors (bugs, violated invariants).
*
Definition at line 458 of file ah-errors.H.
| #define ah_logic_error_unless_constexpr | ( | C | ) |
Definition at line 498 of file ah-errors.H.
| #define ah_out_of_range_error | ( | ) |
Throws std::out_of_range unconditionally.
| std::out_of_range | always |
Throws an out_of_range error without evaluating any condition. Use when you need to throw directly.
*
Definition at line 759 of file ah-errors.H.
| #define ah_out_of_range_error_if | ( | C | ) |
Throws std::out_of_range if condition holds.
| C | Condition to evaluate |
| std::out_of_range | if C is true |
Useful for out of range element access in containers.
*
Definition at line 727 of file ah-errors.H.
| #define ah_out_of_range_error_if_constexpr | ( | C | ) |
Definition at line 763 of file ah-errors.H.
| #define ah_out_of_range_error_unless | ( | C | ) |
Throws std::out_of_range if condition does NOT hold.
| C | Condition to evaluate |
| std::out_of_range | if C is false |
Useful for validating valid index access.
*
Definition at line 743 of file ah-errors.H.
| #define ah_out_of_range_error_unless_constexpr | ( | C | ) |
Definition at line 767 of file ah-errors.H.
| #define ah_overflow_error | ( | ) |
Throws std::overflow_error unconditionally.
| std::overflow_error | always |
Throws an overflow_error without evaluating any condition. Use when you need to throw directly.
*
Definition at line 643 of file ah-errors.H.
| #define ah_overflow_error_if | ( | C | ) |
Throws std::overflow_error if condition holds.
| C | Condition to evaluate |
| std::overflow_error | if C is true |
Useful for arithmetic operations that result in overflow.
*
Definition at line 611 of file ah-errors.H.
| #define ah_overflow_error_if_constexpr | ( | C | ) |
Definition at line 647 of file ah-errors.H.
| #define ah_overflow_error_unless | ( | C | ) |
Throws std::overflow_error if condition does NOT hold.
| C | Condition to evaluate |
| std::overflow_error | if C is false |
Useful for validating absence of arithmetic overflow.
*
Definition at line 627 of file ah-errors.H.
| #define ah_overflow_error_unless_constexpr | ( | C | ) |
Definition at line 651 of file ah-errors.H.
| #define ah_range_error | ( | ) |
Throws std::range_error unconditionally.
| std::range_error | always |
Throws a range_error without evaluating any condition. Use when you need to throw directly.
*
Definition at line 372 of file ah-errors.H.
| #define ah_range_error_if | ( | C | ) |
Throws std::range_error if condition holds.
| C | Condition to evaluate |
| std::range_error | if C is true |
Useful for detecting values out of range.
*
Definition at line 356 of file ah-errors.H.
| #define ah_range_error_if_constexpr | ( | C | ) |
Definition at line 376 of file ah-errors.H.
| #define ah_range_error_unless | ( | C | ) |
Throws std::range_error if condition does NOT hold.
| C | Condition to evaluate |
| std::range_error | if C is false |
Useful for validating that a value is within a valid range. Error message includes location (file:line).
*
Definition at line 340 of file ah-errors.H.
| #define ah_range_error_unless_constexpr | ( | C | ) |
Definition at line 380 of file ah-errors.H.
| #define ah_runtime_error | ( | ) |
Throws std::runtime_error unconditionally.
| std::runtime_error | always |
Throws a runtime_error without evaluating any condition. Use when you need to throw directly.
*
Definition at line 431 of file ah-errors.H.
| #define ah_runtime_error_if | ( | C | ) |
Throws std::runtime_error if condition holds.
| C | Condition to evaluate |
| std::runtime_error | if C is true |
Useful for detecting runtime error conditions.
*
Definition at line 415 of file ah-errors.H.
| #define ah_runtime_error_if_constexpr | ( | C | ) |
Definition at line 435 of file ah-errors.H.
| #define ah_runtime_error_unless | ( | C | ) |
Throws std::runtime_error if condition does NOT hold.
| C | Condition to evaluate |
| std::runtime_error | if C is false |
Useful for errors only detected at runtime.
*
Definition at line 399 of file ah-errors.H.
| #define ah_runtime_error_unless_constexpr | ( | C | ) |
Definition at line 439 of file ah-errors.H.
| #define ah_underflow_error | ( | ) |
Throws std::underflow_error unconditionally.
| std::underflow_error | always |
Throws an underflow_error without evaluating any condition. Use when you need to throw directly.
*
Definition at line 549 of file ah-errors.H.
| #define ah_underflow_error_if | ( | C | ) |
Throws std::underflow_error if condition holds.
| C | Condition to evaluate |
| std::underflow_error | if C is true |
Useful for arithmetic operations that result in underflow.
*
Definition at line 517 of file ah-errors.H.
| #define ah_underflow_error_if_constexpr | ( | C | ) |
Definition at line 553 of file ah-errors.H.
| #define ah_underflow_error_unless | ( | C | ) |
Throws std::underflow_error if condition does NOT hold.
| C | Condition to evaluate |
| std::underflow_error | if C is false |
Useful for validating absence of arithmetic underflow.
*
Definition at line 533 of file ah-errors.H.
| #define ah_underflow_error_unless_constexpr | ( | C | ) |
Definition at line 557 of file ah-errors.H.
Emits an unconditional warning to a stream.
| out | Output stream where to write the warning |
Writes a warning with file and line information without evaluating any condition.
*
Definition at line 321 of file ah-errors.H.
| #define ah_warning_if | ( | out, | |
| C | |||
| ) |
Emits a warning to a stream if the condition holds.
| out | Output stream where to write the warning |
| C | Condition to evaluate |
If condition C is true, writes a warning message with file and line information.
*
Definition at line 305 of file ah-errors.H.
| #define AH_WARNING_ONCE AH_ERRORS_JOIN(ah_warning_once_, __LINE__) |
Definition at line 265 of file ah-errors.H.
| #define ah_warning_unless | ( | out, | |
| C | |||
| ) |
Emits a warning to a stream if the condition does NOT hold.
| out | Output stream where to write the warning |
| C | Condition to evaluate |
If condition C is false, writes a warning message with file and line information. Typical use with std::cerr.
*
Definition at line 288 of file ah-errors.H.