Cantera  2.5.1
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 errorhandling).
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 "cantera/base/fmt.h"
15 #include <exception>
16 
17 namespace Cantera
18 {
19 
20 /*!
21  * @defgroup errorhandling Error Handling
22  *
23  * \brief These classes and related functions are used to handle errors and
24  * unknown events within Cantera.
25  *
26  * The general idea is that exceptions are thrown using the common base class
27  * called CanteraError. Derived types of CanteraError characterize what type of
28  * error is thrown. A list of all of the thrown errors is kept in the
29  * Application class.
30  *
31  * Any exceptions which are not caught cause a fatal error exit from the
32  * program.
33  *
34  * A group of defines may be used during debugging to assert conditions which
35  * should be true. These are named AssertTrace(), AssertThrow(), and
36  * AssertThrowMsg(). Examples of their usage is given below.
37  *
38  * @code
39  * AssertTrace(p == OneAtm);
40  * AssertThrow(p == OneAtm, "Kinetics::update");
41  * AssertThrowMsg(p == OneAtm, "Kinetics::update",
42  * "Algorithm limited to atmospheric pressure");
43  * @endcode
44  *
45  * Their first argument is a boolean. If the boolean is not true, a CanteraError
46  * is thrown, with descriptive information indicating where the error occurred.
47  * The Assert* checks are skipped if the NDEBUG preprocessor symbol is defined,
48  * e.g. with the compiler option -DNDEBUG.
49  */
50 
51 
52 //! Base class for exceptions thrown by Cantera classes.
53 /*!
54  * This class is the base class for exceptions thrown by Cantera. It inherits
55  * from std::exception so that normal error handling operations from
56  * applications may automatically handle the errors in their own way.
57  *
58  * @ingroup errorhandling
59  */
60 class CanteraError : public std::exception
61 {
62 public:
63  //! Normal Constructor for the CanteraError base class
64  /*!
65  * @param procedure Name of the function within which the error was
66  * generated. For member functions, this should be written as
67  * `ClassName::functionName`. For constructors, this should be
68  * `ClassName::ClassName`. Arguments can be specified to
69  * disambiguate overloaded functions, e.g.
70  * `ClassName::functionName(int, int)`.
71  * @param msg Descriptive string describing the type of error message. This
72  * can be a fmt-style format string (i.e. using curly braces to indicate
73  * fields), which will be used with additional arguments to generate a
74  * formatted error message
75  * @param args Arguments which will be used to interpolate the format string
76  */
77  template <typename... Args>
78  CanteraError(const std::string& procedure, const std::string& msg,
79  const Args&... args)
80  : procedure_(procedure)
81  {
82  if (sizeof...(args) == 0) {
83  msg_ = msg;
84  } else {
85  msg_ = fmt::format(msg, args...);
86  }
87  }
88 
89  //! Destructor for base class does nothing
90  virtual ~CanteraError() throw() {};
91 
92  //! Get a description of the error
93  const char* what() const throw();
94 
95  //! Method overridden by derived classes to format the error message
96  virtual std::string getMessage() const;
97 
98  //! Method overridden by derived classes to indicate their type
99  virtual std::string getClass() const {
100  return "CanteraError";
101  }
102 
103 protected:
104  //! Protected default constructor discourages throwing errors containing no
105  //! information.
107 
108  //! Constructor used by derived classes that override getMessage()
109  explicit CanteraError(const std::string& procedure);
110 
111  //! The name of the procedure where the exception occurred
112  std::string procedure_;
113  mutable std::string formattedMessage_; //!< Formatted message returned by what()
114 
115 private:
116  std::string msg_; //!< Message associated with the exception
117 };
118 
119 
120 //! Array size error.
121 /*!
122  * This error is thrown if a supplied length to a vector supplied to Cantera is
123  * too small.
124  *
125  * @ingroup errorhandling
126  */
128 {
129 public:
130  //! Constructor
131  /*!
132  * The length needed is supplied by the argument, reqd, and the
133  * length supplied is given by the argument sz.
134  *
135  * @param procedure String name for the function within which the error was
136  * generated.
137  * @param sz This is the length supplied to Cantera.
138  * @param reqd This is the required length needed by Cantera
139  */
140  ArraySizeError(const std::string& procedure, size_t sz, size_t reqd) :
141  CanteraError(procedure), sz_(sz), reqd_(reqd) {}
142 
143  virtual std::string getMessage() const;
144  virtual std::string getClass() const {
145  return "ArraySizeError";
146  }
147 
148 private:
149  size_t sz_, reqd_;
150 };
151 
152 
153 //! An array index is out of range.
154 /*!
155  * @ingroup errorhandling
156  */
157 class IndexError : public CanteraError
158 {
159 public:
160  //! Constructor
161  /*!
162  * This class indicates an out-of-bounds array index.
163  *
164  * @param func String name for the function within which the error was
165  * generated.
166  * @param arrayName name of the corresponding array
167  * @param m This is the value of the out-of-bounds index.
168  * @param mmax This is the maximum allowed value of the index. The
169  * minimum allowed value is assumed to be 0.
170  */
171  IndexError(const std::string& func, const std::string& arrayName, size_t m, size_t mmax) :
172  CanteraError(func), arrayName_(arrayName), m_(m), mmax_(mmax) {}
173 
174  virtual ~IndexError() throw() {};
175  virtual std::string getMessage() const;
176  virtual std::string getClass() const {
177  return "IndexError";
178  }
179 
180 private:
181  std::string arrayName_;
182  size_t m_, mmax_;
183 };
184 
185 //! An error indicating that an unimplemented function has been called
187 {
188 public:
189  //! @param func Name of the unimplemented function, e.g.
190  //! `ClassName::functionName`
191  NotImplementedError(const std::string& func) :
192  CanteraError(func, "Not implemented.") {}
193 
194  virtual std::string getClass() const {
195  return "NotImplementedError";
196  }
197 };
198 
199 //! Provides a line number
200 #define XSTR_TRACE_LINE(s) STR_TRACE_LINE(s)
201 
202 //! Provides a line number
203 #define STR_TRACE_LINE(s) #s
204 
205 //! Provides a std::string variable containing the file and line number
206 /*!
207  * This is a std:string containing the file name and the line number
208  */
209 #define STR_TRACE (std::string(__FILE__) + ":" + XSTR_TRACE_LINE(__LINE__))
210 
211 #ifdef NDEBUG
212 #ifndef AssertTrace
213 # define AssertTrace(expr) ((void) (0))
214 #endif
215 #ifndef AssertThrow
216 # define AssertThrow(expr, procedure) ((void) (0))
217 #endif
218 #ifndef AssertThrowMsg
219 # define AssertThrowMsg(expr,procedure, ...) ((void) (0))
220 #endif
221 #else
222 
223 //! Assertion must be true or an error is thrown
224 /*!
225  * Assertion must be true or else a CanteraError is thrown. A diagnostic string
226  * containing the file and line number, indicating where the error occurred is
227  * added to the thrown object.
228  *
229  * @param expr Boolean expression that must be true
230  *
231  * @ingroup errorhandling
232  */
233 #ifndef AssertTrace
234 # define AssertTrace(expr) ((expr) ? (void) 0 : throw CanteraError(STR_TRACE, std::string("failed assert: ") + #expr))
235 #endif
236 
237 //! Assertion must be true or an error is thrown
238 /*!
239  * Assertion must be true or else a CanteraError is thrown. A diagnostic string
240  * indicating where the error occurred is added to the thrown object.
241  *
242  * @param expr Boolean expression that must be true
243  * @param procedure Character string or std:string expression indicating the
244  * procedure where the assertion failed
245  * @ingroup errorhandling
246  */
247 #ifndef AssertThrow
248 # define AssertThrow(expr, procedure) ((expr) ? (void) 0 : throw CanteraError(procedure, std::string("failed assert: ") + #expr))
249 #endif
250 
251 //! Assertion must be true or an error is thrown
252 /*!
253  * Assertion must be true or else a CanteraError is thrown. A diagnostic string
254  * indicating where the error occurred is added to the thrown object.
255  *
256  * @param expr Boolean expression that must be true
257  * @param procedure Character string or std:string expression indicating
258  * the procedure where the assertion failed
259  * Additional arguments are passed on to the constructor for CanteraError to
260  * generate a formatted error message.
261  *
262  * @ingroup errorhandling
263  */
264 #ifndef AssertThrowMsg
265 # define AssertThrowMsg(expr, procedure, ...) ((expr) ? (void) 0 : throw CanteraError(procedure + std::string(":\nfailed assert: \"") + std::string(#expr) + std::string("\""), __VA_ARGS__))
266 #endif
267 
268 #endif
269 
270 //! Throw an exception if the specified exception is not a finite number.
271 #ifndef AssertFinite
272 # define AssertFinite(expr, procedure, ...) AssertThrowMsg(expr < BigNumber && expr > -BigNumber, procedure, __VA_ARGS__)
273 #endif
274 
275 }
276 
277 #endif
Array size error.
Definition: ctexceptions.h:128
virtual std::string getMessage() const
Method overridden by derived classes to format the error message.
virtual std::string getClass() const
Method overridden by derived classes to indicate their type.
Definition: ctexceptions.h:144
ArraySizeError(const std::string &procedure, size_t sz, size_t reqd)
Constructor.
Definition: ctexceptions.h:140
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:61
virtual std::string getMessage() const
Method overridden by derived classes to format the error message.
std::string msg_
Message associated with the exception.
Definition: ctexceptions.h:116
virtual std::string getClass() const
Method overridden by derived classes to indicate their type.
Definition: ctexceptions.h:99
std::string procedure_
The name of the procedure where the exception occurred.
Definition: ctexceptions.h:112
const char * what() const
Get a description of the error.
std::string formattedMessage_
Formatted message returned by what()
Definition: ctexceptions.h:113
CanteraError(const std::string &procedure, const std::string &msg, const Args &... args)
Normal Constructor for the CanteraError base class.
Definition: ctexceptions.h:78
virtual ~CanteraError()
Destructor for base class does nothing.
Definition: ctexceptions.h:90
CanteraError()
Protected default constructor discourages throwing errors containing no information.
Definition: ctexceptions.h:106
An array index is out of range.
Definition: ctexceptions.h:158
virtual std::string getMessage() const
Method overridden by derived classes to format the error message.
virtual std::string getClass() const
Method overridden by derived classes to indicate their type.
Definition: ctexceptions.h:176
IndexError(const std::string &func, const std::string &arrayName, size_t m, size_t mmax)
Constructor.
Definition: ctexceptions.h:171
An error indicating that an unimplemented function has been called.
Definition: ctexceptions.h:187
NotImplementedError(const std::string &func)
Definition: ctexceptions.h:191
virtual std::string getClass() const
Method overridden by derived classes to indicate their type.
Definition: ctexceptions.h:194
Wrapper for either system-installed or local headers for fmt.
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:264