Cantera  2.1.2
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 
59 //! Base class for exceptions thrown by Cantera classes.
60 /*!
61  * This class is the base class for exceptions thrown by Cantera.
62  * It inherits from std::exception so that normal error handling
63  * operations from applications may automatically handle the
64  * errors in their own way.
65  *
66  * @ingroup errorhandling
67  */
68 class CanteraError : public std::exception
69 {
70 public:
71  //! Normal Constructor for the CanteraError base class
72  /*!
73  * In the constructor, a call to the Application class is made to store
74  * the strings associated with the generated error condition.
75  *
76  * @param procedure String name for the function within which the error was
77  * generated.
78  * @param msg Descriptive string describing the type of error message.
79  */
80  CanteraError(const std::string& procedure, const std::string& msg);
81 
82  //! Destructor for base class does nothing
83  virtual ~CanteraError() throw() {};
84 
85  //! Get a description of the error
86  const char* what() const throw();
87 
88  //! Function to put this error onto Cantera's error stack
89  void save();
90 
91  //! Method overridden by derived classes to formatted the error message
92  virtual std::string getMessage() const;
93 
94  //! Method overridden by derived classes to indicate their type
95  virtual std::string getClass() const {
96  return "CanteraError";
97  }
98 
99 protected:
100  //! Protected default constructor discourages throwing errors containing no information.
101  CanteraError() : saved_(false) {};
102 
103  //! Constructor used by derived classes that override getMessage()
104  explicit CanteraError(const std::string& procedure);
105 
106  //! The name of the procedure where the exception occurred
107  std::string procedure_;
108 
109 private:
110  std::string msg_; //!< Message associated with the exception
111  mutable std::string formattedMessage_; //!< Formatted message returned by what()
112  bool saved_; //!< Exception has already been saved to Cantera's error stack
113 };
114 
115 
116 //! Array size error.
117 /*!
118  * This error is thrown if a supplied length to a vector supplied
119  * to Cantera is too small.
120  *
121  * @ingroup errorhandling
122  */
124 {
125 public:
126  //! Constructor
127  /*!
128  * The length needed is supplied by the argument, reqd, and the
129  * length supplied is given by the argument sz.
130  *
131  * @param procedure String name for the function within which the error was
132  * generated.
133  * @param sz This is the length supplied to Cantera.
134  * @param reqd This is the required length needed by Cantera
135  */
136  ArraySizeError(const std::string& procedure, size_t sz, size_t reqd) :
137  CanteraError(procedure), sz_(sz), reqd_(reqd) {}
138 
139  virtual std::string getMessage() const;
140  virtual std::string getClass() const {
141  return "ArraySizeError";
142  }
143 
144 private:
145  size_t sz_, reqd_;
146 };
147 
148 
149 //! An array index is out of range.
150 /*!
151  * @ingroup errorhandling
152  */
153 class IndexError : public CanteraError
154 {
155 public:
156  //! Constructor
157  /*!
158  * This class indicates an out-of-bounds array index.
159  *
160  * @param func String name for the function within which the error was
161  * generated.
162  * @param arrayName name of the corresponding array
163  * @param m This is the value of the out-of-bounds index.
164  * @param mmax This is the maximum allowed value of the index. The
165  * minimum allowed value is assumed to be 0.
166  */
167  IndexError(const std::string& func, const std::string& arrayName, size_t m, size_t mmax) :
168  CanteraError(func), arrayName_(arrayName), m_(m), mmax_(mmax) {}
169 
170  virtual ~IndexError() throw() {};
171  virtual std::string getMessage() const;
172  virtual std::string getClass() const {
173  return "IndexError";
174  }
175 
176 private:
177  std::string arrayName_;
178  size_t m_, mmax_;
179 };
180 
181 //! Provides a line number
182 #define XSTR_TRACE_LINE(s) STR_TRACE_LINE(s)
183 
184 //! Provides a line number
185 #define STR_TRACE_LINE(s) #s
186 
187 //! Provides a std::string variable containing the file and line number
188 /*!
189  * This is a std:string containing the file name and the line number
190  */
191 #define STR_TRACE (std::string(__FILE__) + ":" + XSTR_TRACE_LINE(__LINE__))
192 
193 #ifdef NDEBUG
194 #ifndef AssertTrace
195 # define AssertTrace(expr) ((void) (0))
196 #endif
197 #ifndef AssertThrow
198 # define AssertThrow(expr, procedure) ((void) (0))
199 #endif
200 #ifndef AssertThrowMsg
201 # define AssertThrowMsg(expr,procedure, message) ((void) (0))
202 #endif
203 #else
204 
205 //! Assertion must be true or an error is thrown
206 /*!
207  * Assertion must be true or else a CanteraError is thrown. A diagnostic string containing the
208  * file and line number, indicating where the error
209  * occurred is added to the thrown object.
210  *
211  * @param expr Boolean expression that must be true
212  *
213  * @ingroup errorhandling
214  */
215 #ifndef AssertTrace
216 # define AssertTrace(expr) ((expr) ? (void) 0 : throw Cantera::CanteraError(STR_TRACE, std::string("failed assert: ") + #expr))
217 #endif
218 
219 //! Assertion must be true or an error is thrown
220 /*!
221  * Assertion must be true or else a CanteraError is thrown. A diagnostic string indicating where the error
222  * occurred is added to the thrown object.
223  *
224  * @param expr Boolean expression that must be true
225  * @param procedure Character string or std:string expression indicating the procedure where the assertion failed
226  * @ingroup errorhandling
227  */
228 #ifndef AssertThrow
229 # define AssertThrow(expr, procedure) ((expr) ? (void) 0 : throw Cantera::CanteraError(procedure, std::string("failed assert: ") + #expr))
230 #endif
231 
232 //! Assertion must be true or an error is thrown
233 /*!
234  * Assertion must be true or else a CanteraError is thrown. A
235  * diagnostic string indicating where the error occurred is added
236  * to the thrown object.
237  *
238  * @param expr Boolean expression that must be true
239  * @param procedure Character string or std:string expression indicating
240  * the procedure where the assertion failed
241  * @param message Character string or std:string expression containing
242  * a descriptive message is added to the thrown error condition.
243  *
244  * @ingroup errorhandling
245  */
246 #ifndef AssertThrowMsg
247 # define AssertThrowMsg(expr, procedure, message) ((expr) ? (void) 0 : throw Cantera::CanteraError(procedure + std::string(": at failed assert: \"") + std::string(#expr) + std::string("\""), message))
248 #endif
249 
250 #endif
251 
252 //! Throw an exception if the specified exception is not a finite number.
253 #ifndef AssertFinite
254 # define AssertFinite(expr, procedure, message) AssertThrowMsg(expr < BigNumber && expr > -BigNumber, procedure, message)
255 #endif
256 
257 }
258 
259 #endif
IndexError(const std::string &func, const std::string &arrayName, size_t m, size_t mmax)
Constructor.
Definition: ctexceptions.h:167
std::string formattedMessage_
Formatted message returned by what()
Definition: ctexceptions.h:111
bool saved_
Exception has already been saved to Cantera's error stack.
Definition: ctexceptions.h:112
Array size error.
Definition: ctexceptions.h:123
std::string msg_
Message associated with the exception.
Definition: ctexceptions.h:110
virtual std::string getClass() const
Method overridden by derived classes to indicate their type.
Definition: ctexceptions.h:140
virtual std::string getMessage() const
Method overridden by derived classes to formatted 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:107
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:68
virtual std::string getMessage() const
Method overridden by derived classes to formatted the error message.
CanteraError()
Protected default constructor discourages throwing errors containing no information.
Definition: ctexceptions.h:101
virtual std::string getClass() const
Method overridden by derived classes to indicate their type.
Definition: ctexceptions.h:172
virtual std::string getMessage() const
Method overridden by derived classes to formatted the error message.
ArraySizeError(const std::string &procedure, size_t sz, size_t reqd)
Constructor.
Definition: ctexceptions.h:136
virtual std::string getClass() const
Method overridden by derived classes to indicate their type.
Definition: ctexceptions.h:95
An array index is out of range.
Definition: ctexceptions.h:153
void save()
Function to put this error onto Cantera's error stack.
virtual ~CanteraError()
Destructor for base class does nothing.
Definition: ctexceptions.h:83