Cantera  3.1.0
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
167//! An array index is out of range.
168/*!
169 * @ingroup errGroup
170 */
172{
173public:
174 //! Constructor
175 /*!
176 * This class indicates an out-of-bounds array index.
177 *
178 * @param func String name for the function within which the error was
179 * generated.
180 * @param arrayName name of the corresponding array
181 * @param m This is the value of the out-of-bounds index.
182 * @param mmax This is the maximum allowed value of the index. The
183 * minimum allowed value is assumed to be 0.
184 */
185 IndexError(const string& func, const string& arrayName, size_t m, size_t mmax) :
186 CanteraError(func), arrayName_(arrayName), m_(m), mmax_(mmax) {}
187
188 ~IndexError() throw() override {};
189 string getMessage() const override;
190 string getClass() const override {
191 return "IndexError";
192 }
193
194private:
195 string arrayName_;
196 size_t m_, mmax_;
197};
198
199//! An error indicating that an unimplemented function has been called
200//! @ingroup errGroup
202{
203public:
204 //! @param func Name of the unimplemented function, such as
205 //! `ClassName::functionName`
206 NotImplementedError(const string& func) :
207 CanteraError(func, "Not implemented.") {}
208
209 //! Alternative constructor taking same arguments as CanteraError
210 template <typename... Args>
211 NotImplementedError(const string& func, const string& msg, const Args&... args) :
212 CanteraError(func, msg, args...) {}
213
214 string getClass() const override {
215 return "NotImplementedError";
216 }
217};
218
219//! Provides a line number
220#define XSTR_TRACE_LINE(s) STR_TRACE_LINE(s)
221
222//! Provides a line number
223#define STR_TRACE_LINE(s) #s
224
225//! Provides a string variable containing the file and line number
226/*!
227 * This is a std:string containing the file name and the line number
228 */
229#define STR_TRACE (string(__FILE__) + ":" + XSTR_TRACE_LINE(__LINE__))
230
231#ifdef NDEBUG
232#ifndef AssertTrace
233# define AssertTrace(expr) ((void) (0))
234#endif
235#ifndef AssertThrow
236# define AssertThrow(expr, procedure) ((void) (0))
237#endif
238#ifndef AssertThrowMsg
239# define AssertThrowMsg(expr,procedure, ...) ((void) (0))
240#endif
241#else
242
243//! Assertion must be true or an error is thrown
244/*!
245 * Assertion must be true or else a CanteraError is thrown. A diagnostic string
246 * containing the file and line number, indicating where the error occurred is
247 * added to the thrown object.
248 *
249 * @param expr Boolean expression that must be true
250 *
251 * @ingroup errGroup
252 */
253#ifndef AssertTrace
254# define AssertTrace(expr) ((expr) ? (void) 0 : throw CanteraError(STR_TRACE, string("failed assert: ") + #expr))
255#endif
256
257//! Assertion must be true or an error is thrown
258/*!
259 * Assertion must be true or else a CanteraError is thrown. A diagnostic string
260 * indicating where the error occurred is added to the thrown object.
261 *
262 * @param expr Boolean expression that must be true
263 * @param procedure Character string or std:string expression indicating the
264 * procedure where the assertion failed
265 * @ingroup errGroup
266 */
267#ifndef AssertThrow
268# define AssertThrow(expr, procedure) ((expr) ? (void) 0 : throw CanteraError(procedure, string("failed assert: ") + #expr))
269#endif
270
271//! Assertion must be true or an error is thrown
272/*!
273 * Assertion must be true or else a CanteraError is thrown. A diagnostic string
274 * indicating where the error occurred is added to the thrown object.
275 *
276 * @param expr Boolean expression that must be true
277 * @param procedure Character string or std:string expression indicating
278 * the procedure where the assertion failed
279 * Additional arguments are passed on to the constructor for CanteraError to
280 * generate a formatted error message.
281 *
282 * @ingroup errGroup
283 */
284#ifndef AssertThrowMsg
285# define AssertThrowMsg(expr, procedure, ...) ((expr) ? (void) 0 : throw CanteraError(procedure + string(":\nfailed assert: \"") + string(#expr) + string("\""), __VA_ARGS__))
286#endif
287
288#endif
289
290//! Throw an exception if the specified exception is not a finite number.
291//! @ingroup errGroup
292#ifndef AssertFinite
293# define AssertFinite(expr, procedure, ...) AssertThrowMsg(expr < BigNumber && expr > -BigNumber, procedure, __VA_ARGS__)
294#endif
295
296}
297
298#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. 0 to disable.
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.
string getMessage() const override
Method overridden by derived classes to format the error message.
IndexError(const string &func, const string &arrayName, size_t m, size_t mmax)
Constructor.
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