Loading [MathJax]/jax/output/HTML-CSS/config.js
Cantera  3.2.0a1
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 generated.
179 * @param arrayName Name of the corresponding array or empty string @c "".
180 * @param m Value of the out-of-bounds index.
181 * @param arrSize Size of the array.
182 */
183 IndexError(const string& func, const string& arrayName, size_t m, size_t arrSize) :
184 CanteraError(func), arrayName_(arrayName), m_(m), m_size(arrSize) {}
185
186 ~IndexError() throw() override {};
187 string getMessage() const override;
188 string getClass() const override {
189 return "IndexError";
190 }
191
192private:
193 string arrayName_;
194 size_t m_;
195 size_t m_size;
196};
197
198//! An error indicating that an unimplemented function has been called
199//! @ingroup errGroup
201{
202public:
203 //! @param func Name of the unimplemented function, such as
204 //! `ClassName::functionName`
205 NotImplementedError(const string& func) :
206 CanteraError(func, "Not implemented.") {}
207
208 //! Alternative constructor taking same arguments as CanteraError
209 template <typename... Args>
210 NotImplementedError(const string& func, const string& msg, const Args&... args) :
211 CanteraError(func, msg, args...) {}
212
213 string getClass() const override {
214 return "NotImplementedError";
215 }
216};
217
218//! Provides a line number
219#define XSTR_TRACE_LINE(s) STR_TRACE_LINE(s)
220
221//! Provides a line number
222#define STR_TRACE_LINE(s) #s
223
224//! Provides a string variable containing the file and line number
225/*!
226 * This is a std:string containing the file name and the line number
227 */
228#define STR_TRACE (string(__FILE__) + ":" + XSTR_TRACE_LINE(__LINE__))
229
230#ifdef NDEBUG
231#ifndef AssertTrace
232# define AssertTrace(expr) ((void) (0))
233#endif
234#ifndef AssertThrow
235# define AssertThrow(expr, procedure) ((void) (0))
236#endif
237#ifndef AssertThrowMsg
238# define AssertThrowMsg(expr,procedure, ...) ((void) (0))
239#endif
240#else
241
242//! Assertion must be true or an error is thrown
243/*!
244 * Assertion must be true or else a CanteraError is thrown. A diagnostic string
245 * containing the file and line number, indicating where the error occurred is
246 * added to the thrown object.
247 *
248 * @param expr Boolean expression that must be true
249 *
250 * @ingroup errGroup
251 */
252#ifndef AssertTrace
253# define AssertTrace(expr) ((expr) ? (void) 0 : throw CanteraError(STR_TRACE, string("failed assert: ") + #expr))
254#endif
255
256//! Assertion must be true or an error is thrown
257/*!
258 * Assertion must be true or else a CanteraError is thrown. A diagnostic string
259 * indicating where the error occurred is added to the thrown object.
260 *
261 * @param expr Boolean expression that must be true
262 * @param procedure Character string or std:string expression indicating the
263 * procedure where the assertion failed
264 * @ingroup errGroup
265 */
266#ifndef AssertThrow
267# define AssertThrow(expr, procedure) ((expr) ? (void) 0 : throw CanteraError(procedure, string("failed assert: ") + #expr))
268#endif
269
270//! Assertion must be true or an error is thrown
271/*!
272 * Assertion must be true or else a CanteraError is thrown. A diagnostic string
273 * indicating where the error occurred is added to the thrown object.
274 *
275 * @param expr Boolean expression that must be true
276 * @param procedure Character string or std:string expression indicating
277 * the procedure where the assertion failed
278 * Additional arguments are passed on to the constructor for CanteraError to
279 * generate a formatted error message.
280 *
281 * @ingroup errGroup
282 */
283#ifndef AssertThrowMsg
284# define AssertThrowMsg(expr, procedure, ...) ((expr) ? (void) 0 : throw CanteraError(procedure + string(":\nfailed assert: \"") + string(#expr) + string("\""), __VA_ARGS__))
285#endif
286
287#endif
288
289//! Throw an exception if the specified exception is not a finite number.
290//! @ingroup errGroup
291#ifndef AssertFinite
292# define AssertFinite(expr, procedure, ...) AssertThrowMsg(expr < BigNumber && expr > -BigNumber, procedure, __VA_ARGS__)
293#endif
294
295}
296
297#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.
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