Cantera  2.0
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 <iostream>
14 #include <string>
15 
16 namespace Cantera
17 {
18 
19 /*!
20  * @defgroup errorhandling Error Handling
21  *
22  * \brief These classes and related functions are used to handle errors and
23  * unknown events within Cantera.
24  *
25  * The general idea is that exceptions are thrown using the common
26  * base class called CanteraError. Derived types of CanteraError
27  * characterize what type of error is thrown. A list of all
28  * of the thrown errors is kept in the Application class.
29  *
30  * Any exceptions which are not caught cause a fatal error exit
31  * from the program.
32  *
33  * Below is an example of how to catch errors that throw the CanteraError class.
34  * In general, all Cantera C++ programs will have this basic structure.
35  *
36  * \include edemo.cpp
37  *
38  * The function showErrors() will print out the fatal error
39  * condition to standard output.
40  *
41  * A group of defines may be used during debugging to assert
42  * conditions which should be true. These are named AssertTrace(),
43  * AssertThrow(), and AssertThrowMsg(). Examples of their usage is
44  * given below.
45  *
46  * @code
47  * AssertTrace(p == OneAtm);
48  * AssertThrow(p == OneAtm, "Kinetics::update");
49  * AssertThrowMsg(p == OneAtm, "Kinetics::update",
50  * "Algorithm limited to atmospheric pressure");
51  * @endcode
52  *
53  * Their first argument is a boolean. If the boolean is not true, a
54  * CanteraError is thrown, with descriptive information indicating
55  * where the error occurred. These functions may be eliminated from
56  * the source code, if the -DNDEBUG option is specified to the
57  * compiler.
58  */
59 
60 
61 //! Base class for exceptions thrown by Cantera classes.
62 /*!
63  * This class is the base class for exceptions thrown by Cantera.
64  * It inherits from std::exception so that normal error handling
65  * operations from applications may automatically handle the
66  * errors in their own way.
67  *
68  * @ingroup errorhandling
69  */
70 class CanteraError : public std::exception
71 {
72 public:
73  //! Normal Constructor for the CanteraError base class
74  /*!
75  * In the constructor, a call to the Application class is made to store
76  * the strings associated with the generated error condition.
77  *
78  * @param procedure String name for the function within which the error was
79  * generated.
80  * @param msg Descriptive string describing the type of error message.
81  */
82  CanteraError(std::string procedure, std::string msg);
83 
84  //! Destructor for base class does nothing
85  virtual ~CanteraError() throw() {};
86 
87  //! Get a description of the error
88  const char* what() const throw();
89 
90  //! Function to put this error onto Cantera's error stack
91  void save();
92 
93  //! Method overridden by derived classes to formatted the error message
94  virtual std::string getMessage() const;
95 
96  //! Method overridden by derived classes to indicate their type
97  virtual std::string getClass() const { return "CanteraError"; }
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  CanteraError(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(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 { return "ArraySizeError"; }
141 
142 private:
143  size_t sz_, reqd_;
144 };
145 
146 
147 //! An array index is out of range.
148 /*!
149  * @ingroup errorhandling
150  */
151 class IndexError : public CanteraError
152 {
153 public:
154  //! Constructor
155  /*!
156  * This class indicates an out-of-bounds array index.
157  *
158  * @param func String name for the function within which the error was
159  * generated.
160  * @param m This is the value of the out-of-bounds index.
161  * @param mmax This is the maximum allowed value of the index. The
162  * minimum allowed value is assumed to be 0.
163  */
164  IndexError(std::string func, std::string arrayName, size_t m, size_t mmax) :
165  CanteraError(func), arrayName_(arrayName), m_(m), mmax_(mmax) {}
166 
167  virtual ~IndexError() throw() {};
168  virtual std::string getMessage() const;
169  virtual std::string getClass() const { return "IndexError"; }
170 
171 private:
172  std::string arrayName_;
173  size_t m_, mmax_;
174 };
175 
176 
177 //! Print a warning when a deprecated method is called.
178 /*!
179  * These methods are slated to go away in future releases of Cantera.
180  * The developer should work towards eliminating the use of these
181  * methods in the near future.
182  *
183  * @param classnm Class the method belongs to
184  * @param oldnm Name of the deprecated method
185  * @param newnm Name of the method users should use instead
186  *
187  * @ingroup errorhandling
188  */
189 void deprecatedMethod(std::string classnm, std::string oldnm, std::string newnm);
190 
191 //! Throw an error condition for a procedure that has been removed.
192 /*!
193  *
194  * @param func String name for the function within which the error was
195  * generated.
196  * @param version Version of Cantera that first removed this function.
197  *
198  * @ingroup errorhandling
199  */
200 void removeAtVersion(std::string func, std::string version);
201 
202 //! Provides a line number
203 #define XSTR_TRACE_LINE(s) STR_TRACE_LINE(s)
204 
205 //! Provides a line number
206 #define STR_TRACE_LINE(s) #s
207 
208 //! Provides a std::string variable containing the file and line number
209 /*!
210  * This is a std:string containing the file name and the line number
211  */
212 #define STR_TRACE (std::string(__FILE__) + ":" + XSTR_TRACE_LINE(__LINE__))
213 
214 #ifdef NDEBUG
215 #ifndef AssertTrace
216 # define AssertTrace(expr) ((void) (0))
217 #endif
218 #ifndef AssertThrow
219 # define AssertThrow(expr, procedure) ((void) (0))
220 #endif
221 #ifndef AssertThrowMsg
222 # define AssertThrowMsg(expr,procedure, message) ((void) (0))
223 #endif
224 #else
225 
226 //! Assertion must be true or an error is thrown
227 /*!
228  * Assertion must be true or else a CanteraError is thrown. A diagnostic string containing the
229  * file and line number, indicating where the error
230  * occurred is added to the thrown object.
231  *
232  * @param expr Boolean expression that must be true
233  *
234  * @ingroup errorhandling
235  */
236 #ifndef AssertTrace
237 # define AssertTrace(expr) ((expr) ? (void) 0 : throw Cantera::CanteraError(STR_TRACE, std::string("failed assert: ") + #expr))
238 #endif
239 
240 //! Assertion must be true or an error is thrown
241 /*!
242  * Assertion must be true or else a CanteraError is thrown. A diagnostic string indicating where the error
243  * occurred is added to the thrown object.
244  *
245  * @param expr Boolean expression that must be true
246  * @param procedure Character string or std:string expression indicating the procedure where the assertion failed
247  * @ingroup errorhandling
248  */
249 #ifndef AssertThrow
250 # define AssertThrow(expr, procedure) ((expr) ? (void) 0 : throw Cantera::CanteraError(procedure, std::string("failed assert: ") + #expr))
251 #endif
252 
253 //! Assertion must be true or an error is thrown
254 /*!
255  * Assertion must be true or else a CanteraError is thrown. A
256  * diagnostic string indicating where the error occurred is added
257  * to the thrown object.
258  *
259  * @param expr Boolean expression that must be true
260  * @param procedure Character string or std:string expression indicating
261  * the procedure where the assertion failed
262  * @param message Character string or std:string expression containing
263  * a descriptive message is added to the thrown error condition.
264  *
265  * @ingroup errorhandling
266  */
267 #ifndef AssertThrowMsg
268 # define AssertThrowMsg(expr, procedure, message) ((expr) ? (void) 0 : throw Cantera::CanteraError(procedure + std::string(": at failed assert: \"") + std::string(#expr) + std::string("\""), message))
269 #endif
270 
271 
272 
273 #endif
274 
275 }
276 
277 #endif