00001 /** 00002 * @file ctexceptions.h 00003 * Definitions for the classes that are 00004 * thrown when %Cantera experiences an error condition 00005 * (also contains errorhandling module text - see \ref errorhandling). 00006 */ 00007 00008 /* 00009 * $Date: 2010-01-03 19:46:26 -0500 (Sun, 03 Jan 2010) $ 00010 * $Revision: 368 $ 00011 */ 00012 00013 // Copyright 2001 California Institute of Technology 00014 00015 #ifndef CT_CTEXCEPTIONS_H 00016 #define CT_CTEXCEPTIONS_H 00017 00018 #include <string> 00019 #include <exception> 00020 00021 // See file misc.cpp for implementations of methods/functions declared 00022 // here. 00023 00024 namespace Cantera { 00025 00026 /*! 00027 * @defgroup errorhandling Error Handling 00028 * 00029 * \brief These classes and related functions are used to handle errors and 00030 * unknown events within Cantera. 00031 * 00032 * The general idea is that exceptions are thrown using the common 00033 * base class called CanteraError. Derived types of CanteraError 00034 * characterize what type of error is thrown. A list of all 00035 * of the thrown errors is kept in the Application class. 00036 * 00037 * Any exceptions which are not caught cause a fatal error exit 00038 * from the program. 00039 * 00040 * Below is an example of how to catch errors that throw the CanteraError class. 00041 * In general, all Cantera C++ programs will have this basic structure. 00042 * 00043 * \include edemo.cpp 00044 * 00045 * The function showErrors() will print out the fatal error 00046 * condition to standard output. 00047 * 00048 * A group of defines may be used during debugging to assert 00049 * conditions which should be true. These are named AssertTrace(), 00050 * AssertThrow(), and AssertThrowMsg(). Examples of their usage is 00051 * given below. 00052 * 00053 * @code 00054 * AssertTrace(p == OneAtm); 00055 * AssertThrow(p == OneAtm, "Kinetics::update"); 00056 * AssertThrowMsg(p == OneAtm, "Kinetics::update", 00057 * "Algorithm limited to atmospheric pressure"); 00058 * @endcode 00059 * 00060 * Their first argument is a boolean. If the boolean is not true, a 00061 * CanteraError is thrown, with descriptive information indicating 00062 * where the error occured. These functions may be eliminated from 00063 * the source code, if the -DNDEBUG option is specified to the 00064 * compiler. 00065 * 00066 */ 00067 00068 00069 //! Base class for exceptions thrown by Cantera classes. 00070 /*! 00071 * This class is the base class for exceptions thrown by Cantera. 00072 * It inherits from std::exception so that normal error handling 00073 * operations from applications may automatically handle the 00074 * errors in their own way. 00075 * 00076 * @ingroup errorhandling 00077 */ 00078 class CanteraError : public std::exception { 00079 public: 00080 //! Normal Constructor for the CanteraError base class 00081 /*! 00082 * This class doesn't have any storage associated with it. In its 00083 * constructor, a call to the Application class is made to store 00084 * the strings associated with the generated error condition. 00085 * 00086 * @param procedure String name for the function within which the error was 00087 * generated. 00088 * @param msg Descriptive string describing the type of error message. 00089 */ 00090 CanteraError(std::string procedure, std::string msg); 00091 00092 //! Destructor for base class does nothing 00093 virtual ~CanteraError() throw() {} 00094 protected: 00095 //! Empty base constructor is made protected so that it may be used only by 00096 //! inherited classes. 00097 /*! 00098 * We want to discourage throwing an error containing no information. 00099 */ 00100 CanteraError() {} 00101 }; 00102 00103 //! Array size error. 00104 /*! 00105 * This error is thrown if a supplied length to a vector supplied 00106 * to Cantera is too small. 00107 * 00108 * @ingroup errorhandling 00109 */ 00110 class ArraySizeError : public CanteraError { 00111 public: 00112 //! Constructor 00113 /*! 00114 * The length needed is supplied by the argument, reqd, and the 00115 * length supplied is given by the argument sz. 00116 * 00117 * @param procedure String name for the function within which the error was 00118 * generated. 00119 * @param sz This is the length supplied to Cantera. 00120 * @param reqd This is the required length needed by Cantera 00121 */ 00122 ArraySizeError(std::string procedure, int sz, int reqd); 00123 }; 00124 00125 //! An element index is out of range. 00126 /*! 00127 * 00128 * @ingroup errorhandling 00129 */ 00130 class ElementRangeError : public CanteraError { 00131 public: 00132 //! Constructor 00133 /*! 00134 * This class indicates an out-of-bounds index. 00135 * 00136 * @param func String name for the function within which the error was 00137 * generated. 00138 * @param m This is the value of the out-of-bounds index. 00139 * @param mmax This is the maximum allowed value of the index. The 00140 * minimum allowed value is assumed to be 0. 00141 */ 00142 ElementRangeError(std::string func, int m, int mmax); 00143 }; 00144 00145 //! Print a warning when a deprecated method is called. 00146 /*! 00147 * These methods are slated to go away in future releases of Cantera. 00148 * The developer should work towards eliminating the use of these 00149 * methods in the near future. 00150 * 00151 * @param classnm Class the method belongs to 00152 * @param oldnm Name of the deprecated method 00153 * @param newnm Name of the method users should use instead 00154 * 00155 * @ingroup errorhandling 00156 */ 00157 void deprecatedMethod(std::string classnm, std::string oldnm, std::string newnm); 00158 00159 //! Throw an error condition for a procedure that has been removed. 00160 /*! 00161 * 00162 * @param func String name for the function within which the error was 00163 * generated. 00164 * @param version Version of Cantera that first removed this function. 00165 * 00166 * @ingroup errorhandling 00167 */ 00168 void removeAtVersion(std::string func, std::string version); 00169 00170 //! Provides a line number 00171 #define XSTR_TRACE_LINE(s) STR_TRACE_LINE(s) 00172 00173 //! Provides a line number 00174 #define STR_TRACE_LINE(s) #s 00175 00176 //! Provides a std::string variable containing the file and line number 00177 /*! 00178 * This is a std:string containing the file name and the line number 00179 */ 00180 #define STR_TRACE (std::string(__FILE__) + ":" + XSTR_TRACE_LINE(__LINE__)) 00181 00182 #ifdef NDEBUG 00183 #ifndef AssertTrace 00184 # define AssertTrace(expr) ((void) (0)) 00185 #endif 00186 #ifndef AssertThrow 00187 # define AssertThrow(expr, procedure) ((void) (0)) 00188 #endif 00189 #ifndef AssertThrowMsg 00190 # define AssertThrowMsg(expr,procedure, message) ((void) (0)) 00191 #endif 00192 #else 00193 00194 //! Assertion must be true or an error is thrown 00195 /*! 00196 * Assertion must be true or else a CanteraError is thrown. A diagnostic string containing the 00197 * file and line number, indicating where the error 00198 * occured is added to the thrown object. 00199 * 00200 * @param expr Boolean expression that must be true 00201 * 00202 * @ingroup errorhandling 00203 */ 00204 #ifndef AssertTrace 00205 # define AssertTrace(expr) ((expr) ? (void) 0 : throw Cantera::CanteraError(STR_TRACE, std::string("failed assert: ") + #expr)) 00206 #endif 00207 00208 //! Assertion must be true or an error is thrown 00209 /*! 00210 * Assertion must be true or else a CanteraError is thrown. A diagnostic string indicating where the error 00211 * occured is added to the thrown object. 00212 * 00213 * @param expr Boolean expression that must be true 00214 * @param procedure Character string or std:string expression indicating the procedure where the assertion failed 00215 * @ingroup errorhandling 00216 */ 00217 #ifndef AssertThrow 00218 # define AssertThrow(expr, procedure) ((expr) ? (void) 0 : throw Cantera::CanteraError(procedure, std::string("failed assert: ") + #expr)) 00219 #endif 00220 00221 //! Assertion must be true or an error is thrown 00222 /*! 00223 * Assertion must be true or else a CanteraError is thrown. A 00224 * diagnostic string indicating where the error occured is added 00225 * to the thrown object. 00226 * 00227 * @param expr Boolean expression that must be true 00228 * @param procedure Character string or std:string expression indicating 00229 * the procedure where the assertion failed 00230 * @param message Character string or std:string expression contaiing 00231 * a descriptive message is added to the thrown error condition. 00232 * 00233 * @ingroup errorhandling 00234 */ 00235 #ifndef AssertThrowMsg 00236 # define AssertThrowMsg(expr, procedure, message) ((expr) ? (void) 0 : throw Cantera::CanteraError(procedure + std::string(": at failed assert: \"") + std::string(#expr) + std::string("\""), message)) 00237 #endif 00238 00239 00240 00241 #endif 00242 00243 } 00244 00245 #endif