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 cosmological values and all numerical failures
39 use class_test/class_stop (std::runtime_error -> CosmoComputationError, the
40 sampler rejects the point and the chain survives).
41
42 "Possibly-varying" is the operative test, not "numeric". Precision and
43 run-configuration parameters are fixed for a whole run -- a sampler never
44 varies them -- so a numeric check depending only on those is a statement
45 about the CONFIGURATION and belongs in the severe channel. Putting such a
46 check in class_test is actively harmful: whether it fires is then
47 cosmology-dependent, so the sampler reads a broken setup as zero likelihood
48 and silently carves a hole out of the posterior instead of reporting it
49 (#395). The rule is about who can change the operand, not its type. */
50
51#define class_test_severe(condition, args, ...) \
52 { \
53 if (condition) { \
54 ThrowFormattedSevere(__func__, \
55 __LINE__, \
56 "condition (" #condition ") is true; ", \
57 args, \
58 ##__VA_ARGS__); \
59 } \
60 }
61
62#define class_stop_severe(args, ...) \
63 { \
64 ThrowFormattedSevere(__func__, __LINE__, "error; ", args, ##__VA_ARGS__); \
65 }
66
67#define class_open(pointer, filename, mode) \
68 { \
69 (pointer) = fopen(filename, mode); \
70 if ((pointer) == nullptr) { \
71 ThrowFormatted(__func__, \
72 __LINE__, \
73 "", \
74 "could not open %s with name %s and mode %s", \
75 #pointer, \
76 filename, \
77 #mode); \
78 } \
79 }
80
81#endif // CLASS_ERRORS_H