Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 errorhandling).
6  */
7 // Copyright 2001 California Institute of Technology
8 
9 #ifndef CT_CTEXCEPTIONS_H
10 #define CT_CTEXCEPTIONS_H
11 
12 #include <exception>
13 #include <string>
14 
15 namespace Cantera
16 {
17 
18 /*!
19  * @defgroup errorhandling Error Handling
20  *
21  * \brief These classes and related functions are used to handle errors and
22  * unknown events within Cantera.
23  *
24  * The general idea is that exceptions are thrown using the common
25  * base class called CanteraError. Derived types of CanteraError
26  * characterize what type of error is thrown. A list of all
27  * of the thrown errors is kept in the Application class.
28  *
29  * Any exceptions which are not caught cause a fatal error exit
30  * from the program.
31  *
32  * Below is an example of how to catch errors that throw the CanteraError class.
33  * In general, all Cantera C++ programs will have this basic structure.
34  *
35  * \include demo1a.cpp
36  *
37  * The function showErrors() will print out the fatal error
38  * condition to standard output.
39  *
40  * A group of defines may be used during debugging to assert
41  * conditions which should be true. These are named AssertTrace(),
42  * AssertThrow(), and AssertThrowMsg(). Examples of their usage is
43  * given below.
44  *
45  * @code
46  * AssertTrace(p == OneAtm);
47  * AssertThrow(p == OneAtm, "Kinetics::update");
48  * AssertThrowMsg(p == OneAtm, "Kinetics::update",
49  * "Algorithm limited to atmospheric pressure");
50  * @endcode
51  *
52  * Their first argument is a boolean. If the boolean is not true, a
53  * CanteraError is thrown, with descriptive information indicating
54  * where the error occurred. The Assert* checks are skipped if the NDEBUG
55  * preprocessor symbol is defined, e.g. with the compiler option -DNDEBUG.
56  */
57 
58 //! Enum containing Cantera's behavior for situations where overflow or underflow of real variables
59 //! may occur.
60 /*!
61  * Note this frequently occurs when taking exponentials of delta Gibbs energies of reactions
62  * or when taking the exponentials of logs of activity coefficients.
63  */
65 
66  //! For this specification of range behavior, nothing is done. This is the fastest
67  //! behavior when all calculations are believed to be ranged well. For situations
68  //! where there are range errors, NaN's or INF's will be introduced.
70 
71  //! For this specification of range behavior, the overflow or underflow calculation is changed.
72  //! Cantera will proceed by bounding the real number to maintain its viability, silently
73  //! changing the actual answer.
75 
76  //! When an overflow or underflow occurs, Cantera will throw an error
78 
79  //! Cantera will use the fenv check capability introduced in C99 to check for
80  //! overflow and underflow conditions at crucial points.
81  //! It will throw an error if these conditions occur.
83 
84  //! Cantera will throw an error in debug mode but will not in production mode.
85  //! (default)
87 };
88 
89 
90 //! Base class for exceptions thrown by Cantera classes.
91 /*!
92  * This class is the base class for exceptions thrown by Cantera.
93  * It inherits from std::exception so that normal error handling
94  * operations from applications may automatically handle the
95  * errors in their own way.
96  *
97  * @ingroup errorhandling
98  */
99 class CanteraError : public std::exception
100 {
101 public:
102  //! Normal Constructor for the CanteraError base class
103  /*!
104  * In the constructor, a call to the Application class is made to store
105  * the strings associated with the generated error condition.
106  *
107  * @param procedure String name for the function within which the error was
108  * generated.
109  * @param msg Descriptive string describing the type of error message.
110  */
111  CanteraError(const std::string& procedure, const std::string& msg);
112 
113  //! Destructor for base class does nothing
114  virtual ~CanteraError() throw() {};
115 
116  //! Get a description of the error
117  const char* what() const throw();
118 
119  //! Function to put this error onto Cantera's error stack
120  void save();
121 
122  //! Method overridden by derived classes to format the error message
123  virtual std::string getMessage() const;
124 
125  //! Method overridden by derived classes to indicate their type
126  virtual std::string getClass() const {
127  return "CanteraError";
128  }
129 
130 protected:
131  //! Protected default constructor discourages throwing errors containing no information.
132  CanteraError() : saved_(false) {};
133 
134  //! Constructor used by derived classes that override getMessage()
135  explicit CanteraError(const std::string& procedure);
136 
137  //! The name of the procedure where the exception occurred
138  std::string procedure_;
139  mutable std::string formattedMessage_; //!< Formatted message returned by what()
140 
141 private:
142  std::string msg_; //!< Message associated with the exception
143  bool saved_; //!< Exception has already been saved to Cantera's error stack
144 };
145 
146 
147 //! Array size error.
148 /*!
149  * This error is thrown if a supplied length to a vector supplied
150  * to Cantera is too small.
151  *
152  * @ingroup errorhandling
153  */
155 {
156 public:
157  //! Constructor
158  /*!
159  * The length needed is supplied by the argument, reqd, and the
160  * length supplied is given by the argument sz.
161  *
162  * @param procedure String name for the function within which the error was
163  * generated.
164  * @param sz This is the length supplied to Cantera.
165  * @param reqd This is the required length needed by Cantera
166  */
167  ArraySizeError(const std::string& procedure, size_t sz, size_t reqd) :
168  CanteraError(procedure), sz_(sz), reqd_(reqd) {}
169 
170  virtual std::string getMessage() const;
171  virtual std::string getClass() const {
172  return "ArraySizeError";
173  }
174 
175 private:
176  size_t sz_, reqd_;
177 };
178 
179 
180 //! An array index is out of range.
181 /*!
182  * @ingroup errorhandling
183  */
184 class IndexError : public CanteraError
185 {
186 public:
187  //! Constructor
188  /*!
189  * This class indicates an out-of-bounds array index.
190  *
191  * @param func String name for the function within which the error was
192  * generated.
193  * @param arrayName name of the corresponding array
194  * @param m This is the value of the out-of-bounds index.
195  * @param mmax This is the maximum allowed value of the index. The
196  * minimum allowed value is assumed to be 0.
197  */
198  IndexError(const std::string& func, const std::string& arrayName, size_t m, size_t mmax) :
199  CanteraError(func), arrayName_(arrayName), m_(m), mmax_(mmax) {}
200 
201  virtual ~IndexError() throw() {};
202  virtual std::string getMessage() const;
203  virtual std::string getClass() const {
204  return "IndexError";
205  }
206 
207 private:
208  std::string arrayName_;
209  size_t m_, mmax_;
210 };
211 
212 //! An error indicating that an unimplemented function has been called
214 {
215 public:
216  NotImplementedError(const std::string& func) :
217  CanteraError(func, "Not implemented.") {}
218 
219  virtual std::string getClass() const {
220  return "NotImplementedError";
221  }
222 };
223 
224 //! Quick check on whether there has been an underflow or overflow condition in the floating point unit
225 /*!
226  * @return Returns true if there has been such a condition and it has not been cleared. returns false
227  * if there hasn't been an overflow, underflow or invalid condition.
228  */
230 
231 //! Clear all the flags for floating-point exceptions
232 void clear_FENV();
233 
234 
235 //! Provides a line number
236 #define XSTR_TRACE_LINE(s) STR_TRACE_LINE(s)
237 
238 //! Provides a line number
239 #define STR_TRACE_LINE(s) #s
240 
241 //! Provides a std::string variable containing the file and line number
242 /*!
243  * This is a std:string containing the file name and the line number
244  */
245 #define STR_TRACE (std::string(__FILE__) + ":" + XSTR_TRACE_LINE(__LINE__))
246 
247 #ifdef NDEBUG
248 #ifndef AssertTrace
249 # define AssertTrace(expr) ((void) (0))
250 #endif
251 #ifndef AssertThrow
252 # define AssertThrow(expr, procedure) ((void) (0))
253 #endif
254 #ifndef AssertThrowMsg
255 # define AssertThrowMsg(expr,procedure, message) ((void) (0))
256 #endif
257 #else
258 
259 //! Assertion must be true or an error is thrown
260 /*!
261  * Assertion must be true or else a CanteraError is thrown. A diagnostic string containing the
262  * file and line number, indicating where the error
263  * occurred is added to the thrown object.
264  *
265  * @param expr Boolean expression that must be true
266  *
267  * @ingroup errorhandling
268  */
269 #ifndef AssertTrace
270 # define AssertTrace(expr) ((expr) ? (void) 0 : throw Cantera::CanteraError(STR_TRACE, std::string("failed assert: ") + #expr))
271 #endif
272 
273 //! Assertion must be true or an error is thrown
274 /*!
275  * Assertion must be true or else a CanteraError is thrown. A diagnostic string indicating where the error
276  * occurred is added to the thrown object.
277  *
278  * @param expr Boolean expression that must be true
279  * @param procedure Character string or std:string expression indicating the procedure where the assertion failed
280  * @ingroup errorhandling
281  */
282 #ifndef AssertThrow
283 # define AssertThrow(expr, procedure) ((expr) ? (void) 0 : throw Cantera::CanteraError(procedure, std::string("failed assert: ") + #expr))
284 #endif
285 
286 //! Assertion must be true or an error is thrown
287 /*!
288  * Assertion must be true or else a CanteraError is thrown. A
289  * diagnostic string indicating where the error occurred is added
290  * to the thrown object.
291  *
292  * @param expr Boolean expression that must be true
293  * @param procedure Character string or std:string expression indicating
294  * the procedure where the assertion failed
295  * @param message Character string or std:string expression containing
296  * a descriptive message is added to the thrown error condition.
297  *
298  * @ingroup errorhandling
299  */
300 #ifndef AssertThrowMsg
301 # define AssertThrowMsg(expr, procedure, message) ((expr) ? (void) 0 : throw Cantera::CanteraError(procedure + std::string(": at failed assert: \"") + std::string(#expr) + std::string("\""), message))
302 #endif
303 
304 #endif
305 
306 //! Throw an exception if the specified exception is not a finite number.
307 #ifndef AssertFinite
308 # define AssertFinite(expr, procedure, message) AssertThrowMsg(expr < BigNumber && expr > -BigNumber, procedure, message)
309 #endif
310 
311 }
312 
313 #endif
IndexError(const std::string &func, const std::string &arrayName, size_t m, size_t mmax)
Constructor.
Definition: ctexceptions.h:198
std::string formattedMessage_
Formatted message returned by what()
Definition: ctexceptions.h:139
bool saved_
Exception has already been saved to Cantera's error stack.
Definition: ctexceptions.h:143
Array size error.
Definition: ctexceptions.h:154
An error indicating that an unimplemented function has been called.
Definition: ctexceptions.h:213
For this specification of range behavior, the overflow or underflow calculation is changed...
Definition: ctexceptions.h:74
std::string msg_
Message associated with the exception.
Definition: ctexceptions.h:142
When an overflow or underflow occurs, Cantera will throw an error.
Definition: ctexceptions.h:77
CT_RealNumber_Range_Behavior
Enum containing Cantera's behavior for situations where overflow or underflow of real variables may o...
Definition: ctexceptions.h:64
virtual std::string getClass() const
Method overridden by derived classes to indicate their type.
Definition: ctexceptions.h:171
virtual std::string getClass() const
Method overridden by derived classes to indicate their type.
Definition: ctexceptions.h:219
virtual std::string getMessage() const
Method overridden by derived classes to format the error message.
const char * what() const
Get a description of the error.
std::string procedure_
The name of the procedure where the exception occurred.
Definition: ctexceptions.h:138
bool check_FENV_OverUnder_Flow()
Quick check on whether there has been an underflow or overflow condition in the floating point unit...
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:99
virtual std::string getMessage() const
Method overridden by derived classes to format the error message.
CanteraError()
Protected default constructor discourages throwing errors containing no information.
Definition: ctexceptions.h:132
void clear_FENV()
Clear all the flags for floating-point exceptions.
virtual std::string getClass() const
Method overridden by derived classes to indicate their type.
Definition: ctexceptions.h:203
virtual std::string getMessage() const
Method overridden by derived classes to format the error message.
ArraySizeError(const std::string &procedure, size_t sz, size_t reqd)
Constructor.
Definition: ctexceptions.h:167
virtual std::string getClass() const
Method overridden by derived classes to indicate their type.
Definition: ctexceptions.h:126
For this specification of range behavior, nothing is done.
Definition: ctexceptions.h:69
An array index is out of range.
Definition: ctexceptions.h:184
Cantera will throw an error in debug mode but will not in production mode.
Definition: ctexceptions.h:86
Cantera will use the fenv check capability introduced in C99 to check for overflow and underflow cond...
Definition: ctexceptions.h:82
void save()
Function to put this error onto Cantera's error stack.
virtual ~CanteraError()
Destructor for base class does nothing.
Definition: ctexceptions.h:114