CLASSpp Manual
Cosmology reference and developer manual
Loading...
Searching...
No Matches
errors.h
Go to the documentation of this file.
1
2#ifndef CLASS_ERRORS_H
3#define CLASS_ERRORS_H
4
5#include <cstdio> // fopen / nullptr-compare in class_open
6
7[[noreturn]] void ThrowFormatted(
8 const char* func, int line, const char* prefix, const char* fmt, ...);
9
10[[noreturn]] void ThrowFormattedSevere(
11 const char* func, int line, const char* prefix, const char* fmt, ...);
12
13/* class_test / class_stop throw std::runtime_error from the error origin
14 (computation errors). The severe variants further down throw
15 std::invalid_argument; see their comment block for which to use when. */
16
17#define class_test(condition, args, ...) \
18 { \
19 if (condition) { \
20 ThrowFormatted(__func__, \
21 __LINE__, \
22 "condition (" #condition ") is true; ", \
23 args, \
24 ##__VA_ARGS__); \
25 } \
26 }
27
28#define class_stop(args, ...) \
29 { \
30 ThrowFormatted(__func__, __LINE__, "error; ", args, ##__VA_ARGS__); \
31 }
32
33/* Severe variants: throw std::invalid_argument (classy: CosmoSevereError, the
34 sampler ABORTS). Use ONLY for checks that depend exclusively on the
35 STRUCTURE of the input: key presence, mutual exclusivity, unparseable
36 strings, unknown enum names, list-length/count consistency, API-argument
37 validation. A severe check must never depend on possibly-varying (numeric)
38 parameters: range checks on parsed values and all numerical failures use
39 class_test/class_stop (std::runtime_error -> CosmoComputationError, the
40 sampler rejects the point and the chain survives). */
41
42#define class_test_severe(condition, args, ...) \
43 { \
44 if (condition) { \
45 ThrowFormattedSevere(__func__, \
46 __LINE__, \
47 "condition (" #condition ") is true; ", \
48 args, \
49 ##__VA_ARGS__); \
50 } \
51 }
52
53#define class_stop_severe(args, ...) \
54 { \
55 ThrowFormattedSevere(__func__, __LINE__, "error; ", args, ##__VA_ARGS__); \
56 }
57
58#define class_open(pointer, filename, mode) \
59 { \
60 pointer = fopen(filename, mode); \
61 if (pointer == nullptr) { \
62 ThrowFormatted(__func__, \
63 __LINE__, \
64 "", \
65 "could not open %s with name %s and mode %s", \
66 #pointer, \
67 filename, \
68 #mode); \
69 } \
70 }
71
72#endif // CLASS_ERRORS_H