1 // -*- C++ -*- 2 /** 3 * @brief Exception defines top-most exception strategies 4 * 5 * Copyright 2005-2021 Airbus-EDF-IMACS-ONERA-Phimeca 6 * 7 * This library is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU Lesser General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public License 18 * along with this library. If not, see <http://www.gnu.org/licenses/>. 19 * 20 */ 21 #ifndef OPENTURNS_EXCEPTION_HXX 22 #define OPENTURNS_EXCEPTION_HXX 23 24 #include <exception> 25 #include "openturns/OTprivate.hxx" 26 #include "openturns/OSS.hxx" 27 28 BEGIN_NAMESPACE_OPENTURNS 29 30 #ifndef SWIG 31 /* 32 * A helper class that saves the position where it was instanciated in file. 33 * This class works in conjonction with the preprocessor (cpp). See HERE macro 34 */ 35 class OT_API PointInSourceFile 36 { 37 public: PointInSourceFile(const char * file,int line)38 PointInSourceFile(const char * file, int line 39 ) 40 : file_(file), line_(line) 41 {} ~PointInSourceFile()42 ~PointInSourceFile() {} 43 getFile() const44 const char * getFile() const 45 { 46 return file_; 47 } getLine() const48 int getLine() const 49 { 50 return line_; 51 } 52 str() const53 std::string str() const 54 { 55 std::ostringstream oss; 56 oss << file_ << ":" << line_ 57 ; 58 return oss.str(); 59 } 60 61 private: 62 const char * file_; 63 int line_; 64 }; /* class PointInSourceFile */ 65 66 67 /* 68 * The macro HERE creates a PointInSourceFile object that saves the position 69 * in the source file. 70 */ 71 72 #define HERE OT::PointInSourceFile(__FILE__,__LINE__) 73 74 #endif /* SWIG */ 75 76 77 78 79 80 81 /** 82 * @class Exception 83 * 84 * @brief The base class for all OpenTURNS' exceptions 85 * 86 * Exception defines top-most exception strategies. It derives from STL std::exception 87 * so OpenTURNS' exceptions can be catched as standard exceptions. 88 */ 89 90 class OT_API Exception : 91 public std::exception 92 { 93 94 public: 95 96 /** Default constructor 97 * @internal 98 */ 99 Exception(const PointInSourceFile & point); 100 101 /** Copy constructor */ 102 Exception(const Exception & other); 103 104 /** Destructor */ 105 virtual ~Exception() throw(); 106 107 /** @copydoc Object::__repr__() const */ 108 String __repr__() const throw(); 109 110 /** Point accessor 111 * 112 * This method returns a string describing where the exception was launched. 113 * No need to free the string. 114 */ 115 const char * where() const throw(); 116 117 /** Reason accessor 118 * 119 * This method returns a string describing what was the reason of the exception. 120 * No need to free the string. 121 */ 122 const char * what() const throw(); 123 124 /** Class name accessor 125 * 126 * This method returns a string containing the class of the exception. 127 * No need to free the string. 128 */ 129 const char * type() const throw(); 130 131 /** Stream operator 132 * 133 * This method makes exceptions as friendly as std::ostream to add information. 134 * @param obj Any object that can be streamed out to an std::ostream can be appended to the exception's reason 135 */ operator <<(T obj)136 template <class T> Exception & operator << (T obj) 137 { 138 reason_ += OSS() << obj; 139 return *this; 140 } 141 142 143 protected: 144 145 /* Inheritance constructor */ 146 Exception(const PointInSourceFile & point, const char * type); 147 148 private: 149 150 /** The point in source file where the exception was raised */ 151 const PointInSourceFile point_; 152 153 /** Reason describes what was the exception */ 154 String reason_; 155 156 /** Exception class name */ 157 const char * className_; 158 159 }; /* class Exception */ 160 161 /** 162 * @fn std::ostream & operator <<(std::ostream & os, const Exception & obj) 163 * @brief Output stream converter 164 * @param os A STL output stream exception 165 * @param obj The exception read by \em os 166 * @return A reference to \em os 167 * 168 * Operator << converts the Exception object to an output stream 169 * so it is easy to show the reason of the exception. 170 */ 171 OT_API std::ostream & operator <<(std::ostream & os, const Exception & obj); 172 #ifndef SWIG 173 OT_API OStream & operator <<(OStream & OS, const Exception & obj); 174 #endif 175 176 177 178 179 180 /* 181 * 182 * All exceptions defined for the user are listed below 183 * 184 */ 185 186 187 #define NEW_EXCEPTION( CName ) class OT_API CName : public Exception \ 188 { \ 189 public: \ 190 CName (const PointInSourceFile & point); \ 191 virtual ~CName () throw(); \ 192 template <class T> CName & operator << (T obj) \ 193 { \ 194 this->Exception::operator << ( obj ); \ 195 return *this; \ 196 } \ 197 } 198 199 200 /** 201 * @class FileNotFoundException 202 * @brief Raised when a file can not be found on filesystem 203 * @internal 204 */ 205 NEW_EXCEPTION( FileNotFoundException ); 206 207 /** 208 * @class InternalException 209 * @brief Raised when an internal failure happened preventing processing termination 210 * @internal 211 */ 212 NEW_EXCEPTION( InternalException ); 213 214 /** 215 * @class InvalidArgumentException 216 * @brief Raised when an incorrect argument is passed to a method 217 * @internal 218 */ 219 NEW_EXCEPTION( InvalidArgumentException ); 220 221 /** 222 * @class InvalidDimensionException 223 * @brief Raised when an abject of an incorrect dimension is passed to a method 224 * @internal 225 */ 226 NEW_EXCEPTION( InvalidDimensionException ); 227 228 /** 229 * @class NotYetImplementedException 230 * @brief Raised when a method has not been implemented yet though it should be 231 * @internal 232 */ 233 NEW_EXCEPTION( NotYetImplementedException ); 234 235 /** 236 * @class OutOfBoundException 237 * @brief Raised when an index of a collection jumped out of the expected bounds 238 * @internal 239 */ 240 NEW_EXCEPTION( OutOfBoundException ); 241 242 /** 243 * @class XMLException 244 * @brief Raised when a general XML error was detected 245 * @internal 246 */ 247 NEW_EXCEPTION( XMLException ); 248 249 /** 250 * @class XMLParserException 251 * @brief Raised when an XML error was detected during file parsing 252 * @internal 253 */ 254 NEW_EXCEPTION( XMLParserException ); 255 256 /** 257 * @class DynamicLibraryException 258 * @brief Raised when a dynamic library can not be loaded or when a symbol can not be found in it 259 * @internal 260 */ 261 NEW_EXCEPTION( DynamicLibraryException ); 262 263 /** 264 * @class NotSymmetricDefinitePositiveException 265 * @brief Raised when a matrix seems not to be symmetric nor positive definite 266 * @internal 267 */ 268 NEW_EXCEPTION( NotSymmetricDefinitePositiveException ); 269 270 /** 271 * @class InvalidRangeException 272 * @brief Raised when an incorrect 273 * @internal 274 */ 275 NEW_EXCEPTION( InvalidRangeException ); 276 277 /** 278 * @class NotDefinedException 279 * @brief Raised when raise when a method has no definition and should not have one 280 * @internal 281 */ 282 NEW_EXCEPTION( NotDefinedException ); 283 284 /** 285 * @class FileOpenException 286 * @brief Raised when a file can not be opened 287 * @internal 288 */ 289 NEW_EXCEPTION( FileOpenException ); 290 291 /** 292 * @class StudyFileParsingException 293 * @brief Raised when the study file can be read 294 * @internal 295 */ 296 NEW_EXCEPTION( StudyFileParsingException ); 297 298 /** 299 * @class ObjectNotInStudyException 300 * @brief Raised when a file does not belong to a saved study 301 * @internal 302 */ 303 NEW_EXCEPTION( ObjectNotInStudyException ); 304 305 /** 306 * @class ConfigurationFileParsingException 307 * @brief Raised when the configuration file can not be read 308 * @internal 309 */ 310 NEW_EXCEPTION( ConfigurationFileParsingException ); 311 312 313 #undef NEW_EXCEPTION 314 315 END_NAMESPACE_OPENTURNS 316 317 #endif /* OPENTURNS_EXCEPTION_HXX */ 318