Cantera  4.0.0a1
Loading...
Searching...
No Matches
ctexceptions.h
Go to the documentation of this file.
1/**
2 * @file ctexceptions.h
3 * Definitions for the classes that are
4 * thrown when %Cantera experiences an error condition
5 * (also contains errorhandling module text - see @ref errGroup).
6 */
7
8// This file is part of Cantera. See License.txt in the top-level directory or
9// at https://cantera.org/license.txt for license and copyright information.
10
11#ifndef CT_CTEXCEPTIONS_H
12#define CT_CTEXCEPTIONS_H
13
14#include "ct_defs.h"
15#include "cantera/base/fmt.h"
16#include <exception>
17
18namespace Cantera
19{
20
21/**
22 * @defgroup debugGroup Errors and Diagnostics
23*/
24
25/**
26 * @defgroup errGroup Errors
27 *
28 * @brief Handling of errors and unknown events within %Cantera.
29 *
30 * The general idea is that exceptions are thrown using the common base class
31 * called CanteraError. Derived types of CanteraError characterize what type of
32 * error is thrown. A list of all of the thrown errors is kept in the
33 * Application class.
34 *
35 * Any exceptions which are not caught cause a fatal error exit from the
36 * program.
37 *
38 * A group of defines may be used during debugging to assert conditions which
39 * should be true. These are named AssertTrace(), AssertThrow(), and
40 * AssertThrowMsg(). Examples of their usage is given below.
41 *
42 * @code
43 * AssertTrace(p == OneAtm);
44 * AssertThrow(p == OneAtm, "Kinetics::update");
45 * AssertThrowMsg(p == OneAtm, "Kinetics::update",
46 * "Algorithm limited to atmospheric pressure");
47 * @endcode
48 *
49 * Their first argument is a boolean. If the boolean is not true, a CanteraError
50 * is thrown, with descriptive information indicating where the error occurred.
51 * The Assert* checks are skipped if the NDEBUG preprocessor symbol is defined,
52 * for example with the compiler option -DNDEBUG.
53 * @ingroup debugGroup
54 */
55
56
57//! Base class for exceptions thrown by %Cantera classes.
58/*!
59 * This class is the base class for exceptions thrown by Cantera. It inherits
60 * from std::exception so that normal error handling operations from
61 * applications may automatically handle the errors in their own way.
62 *
63 * @ingroup errGroup
64 */
65class CanteraError : public std::exception
66{
67public:
68 //! Normal Constructor for the CanteraError base class
69 /*!
70 * @param procedure Name of the function within which the error was
71 * generated. For member functions, this should be written as
72 * `ClassName::functionName`. For constructors, this should be
73 * `ClassName::ClassName`. Arguments can be specified to
74 * disambiguate overloaded functions, such as
75 * `ClassName::functionName(int, int)`.
76 * @param msg Descriptive string describing the type of error message. This
77 * can be a fmt-style format string (that is, using curly braces to indicate
78 * fields), which will be used with additional arguments to generate a
79 * formatted error message
80 * @param args Arguments which will be used to interpolate the format string
81 */
82 template <typename... Args>
83 CanteraError(const string& procedure, const string& msg, const Args&... args)
84 : CanteraError(procedure)
85 {
86 if (sizeof...(args) == 0) {
87 msg_ = msg;
88 } else {
89 msg_ = fmt::format(fmt::runtime(msg), args...);
90 }
91 }
92
93 //! Destructor for base class does nothing
94 virtual ~CanteraError() throw() {};
95
96 //! Get a description of the error
97 const char* what() const throw() override;
98
99 //! Method overridden by derived classes to format the error message
100 virtual string getMessage() const;
101
102 //! Get the name of the method that threw the exception
103 virtual string getMethod() const;
104
105 //! Method overridden by derived classes to indicate their type
106 virtual string getClass() const {
107 return "CanteraError";
108 }
109
110 //! Set the number of stack frames to include when a CanteraError is displayed. By
111 //! default, or if the depth is set to 0, no stack information will be shown.
112 static void setStackTraceDepth(int depth);
113
114protected:
115 //! Protected default constructor discourages throwing errors containing no
116 //! information.
118
119 //! Constructor used by derived classes that override getMessage()
120 explicit CanteraError(const string& procedure);
121
122 //! The name of the procedure where the exception occurred
124 mutable string formattedMessage_; //!< Formatted message returned by what()
125
126private:
127 string msg_; //!< Message associated with the exception
128
129 string traceback_; //!< Stack trace to location where exception was thrown
130 static int traceDepth_; //!< Number of stack frames to show. 0 to disable.
131};
132
133
134//! Array size error.
135/*!
136 * This error is thrown if a supplied length to a vector supplied to %Cantera is
137 * too small.
138 *
139 * @ingroup errGroup
140 */
142{
143public:
144 //! Constructor
145 /*!
146 * The length needed is supplied by the argument, reqd, and the
147 * length supplied is given by the argument sz.
148 *
149 * @param procedure String name for the function within which the error was
150 * generated.
151 * @param sz This is the length supplied to Cantera.
152 * @param reqd This is the required length needed by Cantera
153 */
154 ArraySizeError(const string& procedure, size_t sz, size_t reqd) :
155 CanteraError(procedure), sz_(sz), reqd_(reqd) {}
156
157 string getMessage() const override;
158 string getClass() const override {
159 return "ArraySizeError";
160 }
161
162private:
163 size_t sz_, reqd_;
164};
165
166//! Wrapper for throwing ArraySizeError.
167//! Used to reduce boilerplate implementation and spurious lack of code coverage.
168inline void checkArraySize(const char* procedure, size_t available, size_t required)
169{
170 if (required > available) {
171 throw ArraySizeError(procedure, available, required);
172 }
173}
174
175//! An array index is out of range.
176/*!
177 * @ingroup errGroup
178 */
180{
181public:
182 //! Constructor
183 /*!
184 * This class indicates an out-of-bounds array index.
185 *
186 * @param func String name for the function within which the error was generated.
187 * @param arrayName Name of the corresponding array or empty string @c "".
188 * @param m Value of the out-of-bounds index.
189 * @param arrSize Size of the array.
190 */
191 IndexError(const string& func, const string& arrayName, size_t m, size_t arrSize) :
192 CanteraError(func), arrayName_(arrayName), m_(m), m_size(arrSize) {}
193
194 ~IndexError() throw() override {};
195 string getMessage() const override;
196 string getClass() const override {
197 return "IndexError";
198 }
199
200private:
201 string arrayName_;
202 size_t m_;
203 size_t m_size;
204};
205
206//! An error indicating that an unimplemented function has been called
207//! @ingroup errGroup
209{
210public:
211 //! @param func Name of the unimplemented function, such as
212 //! `ClassName::functionName`
213 NotImplementedError(const string& func) :
214 CanteraError(func, "Not implemented.") {}
215
216 //! Alternative constructor taking same arguments as CanteraError
217 template <typename... Args>
218 NotImplementedError(const string& func, const string& msg, const Args&... args) :
219 CanteraError(func, msg, args...) {}
220
221 string getClass() const override {
222 return "NotImplementedError";
223 }
224};
225
226//! Provides a line number
227#define XSTR_TRACE_LINE(s) STR_TRACE_LINE(s)
228
229//! Provides a line number
230#define STR_TRACE_LINE(s) #s
231
232//! Provides a string variable containing the file and line number
233/*!
234 * This is a std:string containing the file name and the line number
235 */
236#define STR_TRACE (string(__FILE__) + ":" + XSTR_TRACE_LINE(__LINE__))
237
238#ifdef NDEBUG
239#ifndef AssertTrace
240# define AssertTrace(expr) ((void) (0))
241#endif
242#ifndef AssertThrow
243# define AssertThrow(expr, procedure) ((void) (0))
244#endif
245#ifndef AssertThrowMsg
246# define AssertThrowMsg(expr,procedure, ...) ((void) (0))
247#endif
248#else
249
250//! Assertion must be true or an error is thrown
251/*!
252 * Assertion must be true or else a CanteraError is thrown. A diagnostic string
253 * containing the file and line number, indicating where the error occurred is
254 * added to the thrown object.
255 *
256 * @param expr Boolean expression that must be true
257 *
258 * @ingroup errGroup
259 */
260#ifndef AssertTrace
261# define AssertTrace(expr) ((expr) ? (void) 0 : throw CanteraError(STR_TRACE, string("failed assert: ") + #expr))
262#endif
263
264//! Assertion must be true or an error is thrown
265/*!
266 * Assertion must be true or else a CanteraError is thrown. A diagnostic string
267 * indicating where the error occurred is added to the thrown object.
268 *
269 * @param expr Boolean expression that must be true
270 * @param procedure Character string or std:string expression indicating the
271 * procedure where the assertion failed
272 * @ingroup errGroup
273 */
274#ifndef AssertThrow
275# define AssertThrow(expr, procedure) ((expr) ? (void) 0 : throw CanteraError(procedure, string("failed assert: ") + #expr))
276#endif
277
278//! Assertion must be true or an error is thrown
279/*!
280 * Assertion must be true or else a CanteraError is thrown. A diagnostic string
281 * indicating where the error occurred is added to the thrown object.
282 *
283 * @param expr Boolean expression that must be true
284 * @param procedure Character string or std:string expression indicating
285 * the procedure where the assertion failed
286 * Additional arguments are passed on to the constructor for CanteraError to
287 * generate a formatted error message.
288 *
289 * @ingroup errGroup
290 */
291#ifndef AssertThrowMsg
292# define AssertThrowMsg(expr, procedure, ...) ((expr) ? (void) 0 : throw CanteraError(procedure + string(":\nfailed assert: \"") + string(#expr) + string("\""), __VA_ARGS__))
293#endif
294
295#endif
296
297//! Throw an exception if the specified exception is not a finite number.
298//! @ingroup errGroup
299#ifndef AssertFinite
300# define AssertFinite(expr, procedure, ...) AssertThrowMsg(expr < BigNumber && expr > -BigNumber, procedure, __VA_ARGS__)
301#endif
302
303}
304
305#endif
Array size error.
ArraySizeError(const string &procedure, size_t sz, size_t reqd)
Constructor.
string getMessage() const override
Method overridden by derived classes to format the error message.
string getClass() const override
Method overridden by derived classes to indicate their type.
Base class for exceptions thrown by Cantera classes.
const char * what() const override
Get a description of the error.
string traceback_
Stack trace to location where exception was thrown.
virtual string getMessage() const
Method overridden by derived classes to format the error message.
string formattedMessage_
Formatted message returned by what()
static int traceDepth_
Number of stack frames to show.
string procedure_
The name of the procedure where the exception occurred.
virtual ~CanteraError()
Destructor for base class does nothing.
CanteraError()
Protected default constructor discourages throwing errors containing no information.
string msg_
Message associated with the exception.
static void setStackTraceDepth(int depth)
Set the number of stack frames to include when a CanteraError is displayed.
virtual string getMethod() const
Get the name of the method that threw the exception.
CanteraError(const string &procedure, const string &msg, const Args &... args)
Normal Constructor for the CanteraError base class.
virtual string getClass() const
Method overridden by derived classes to indicate their type.
An array index is out of range.
IndexError(const string &func, const string &arrayName, size_t m, size_t arrSize)
Constructor.
string getMessage() const override
Method overridden by derived classes to format the error message.
string getClass() const override
Method overridden by derived classes to indicate their type.
An error indicating that an unimplemented function has been called.
NotImplementedError(const string &func, const string &msg, const Args &... args)
Alternative constructor taking same arguments as CanteraError.
NotImplementedError(const string &func)
string getClass() const override
Method overridden by derived classes to indicate their type.
This file contains definitions of constants, types and terms that are used in internal routines and a...
Wrapper for either system-installed or local headers for fmt.
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
void checkArraySize(const char *procedure, size_t available, size_t required)
Wrapper for throwing ArraySizeError.