Changeset 1120:5d8d64bde9c5 in lemon-0.x
- Timestamp:
- 02/03/05 20:24:42 (20 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1519
- Location:
- src/work/klao
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/work/klao/error.h
r1068 r1120 103 103 */ 104 104 class Exception : public std::exception { 105 protected: 106 ///\e 107 const char *message; 108 109 public: 110 ///\e 111 Exception() throw() : message(0) {} 112 ///\e 113 explicit Exception(const char *msg) throw() 114 : message(msg) {} 105 public: 106 ///\e 107 Exception() {} 115 108 ///\e 116 109 virtual ~Exception() throw() {} 110 111 ///\e 112 virtual const char* exceptionName() const { 113 return "lemon::Exception"; 114 } 117 115 118 116 ///\e 119 117 virtual const char* what() const throw() { 120 if( message ) return message; 121 return "lemon::Exception"; 122 } 123 }; 124 125 ///\e 118 return exceptionName(); 119 } 120 }; 121 122 /** 123 * \brief One of the two main subclasses of \ref Exception. 124 * 125 * Logic errors represent problems in the internal logic of a program; 126 * in theory, these are preventable, and even detectable before the 127 * program runs (e.g., violations of class invariants). 128 * 129 * For a typical example \see UninitializedParameterError. 130 */ 126 131 class LogicError : public Exception { 127 132 public: 128 ///\e 129 explicit LogicError() {} 130 ///\e 131 explicit LogicError(const char *s) 132 : Exception(s) {} 133 }; 134 135 ///\e 133 virtual const char* exceptionName() const { 134 return "lemon::LogicError"; 135 } 136 }; 137 138 139 /** 140 * \brief One of the two main subclasses of \ref Exception. 141 * 142 * Runtime errors represent problems outside the scope of a program; 143 * they cannot be easily predicted and can generally only be caught as 144 * the program executes. 145 */ 136 146 class RuntimeError : public Exception { 137 147 public: 138 ///\e 139 explicit RuntimeError() {} 140 ///\e 141 explicit RuntimeError(const char *s) 142 : Exception(s) {} 148 virtual const char* exceptionName() const { 149 return "lemon::RuntimeError"; 150 } 143 151 }; 144 152 … … 146 154 class RangeError : public RuntimeError { 147 155 public: 148 ///\e149 explicit RangeError(const char *s)150 : RuntimeError(s) {}156 virtual const char* exceptionName() const { 157 return "lemon::RangeError"; 158 } 151 159 }; 152 160 … … 154 162 class IOError : public RuntimeError { 155 163 public: 156 ///\e157 explicit IOError(const char *s)158 : RuntimeError(s) {}164 virtual const char* exceptionName() const { 165 return "lemon::IOError"; 166 } 159 167 }; 160 168 … … 162 170 class DataFormatError : public IOError { 163 171 protected: 164 int line; 165 boost::shared_ptr<std::string> file; 166 167 public: 168 ///\e 169 explicit DataFormatError(const char *message) 170 : IOError(message), line(0) {} 172 const char *_message; 173 int _line; 174 boost::shared_ptr<std::string> _file; 175 176 public: 177 ///\e 178 explicit DataFormatError(const char *the_message) 179 : _message(the_message), _line(0) {} 171 180 ///\e 172 181 DataFormatError(const std::string &file_name, int line_num, 173 const char *message) 174 : IOError(message), line(line_num) { set_file(file_name); } 175 176 ///\e 177 void set_line(int line_num) { line=line_num; } 178 ///\e 179 void set_file(const std::string &file_name) { 180 try { 181 file.reset(new std::string); 182 *file = file_name; 182 const char *the_message) 183 : _message(the_message), _line(line_num) { file(file_name); } 184 185 ///\e 186 void line(int line_num) { _line=line_num; } 187 ///\e 188 void message(char *the_message) { _message=the_message; } 189 ///\e 190 void file(const std::string &file_name) { 191 try { 192 _file.reset(new std::string); 193 *_file = file_name; 183 194 } 184 195 catch(...) { 185 file.reset(); 186 } 187 } 188 189 ///\e 190 int get_line() const { return line; } 196 _file.reset(); 197 } 198 } 199 200 ///\e 201 int line() const { return _line; } 202 ///\e 203 const char* message() const { return _message; } 191 204 192 205 /// \brief Returns the filename. 193 206 /// 194 207 /// Returns \e "(unknown)" if the filename was not specified. 195 const char* get_file() const {196 if( file )197 return file->c_str();208 const char* file() const { 209 if( _file ) 210 return _file->c_str(); 198 211 else 199 212 return "(unknown)"; … … 205 218 try { 206 219 std::ostringstream ostr; 207 ostr << IOError::what();208 if( file ||line ) {220 ostr << _message; 221 if( _file || _line ) { 209 222 ostr << " ("; 210 if( file ) ostr << "in file '" << *file << "'";211 if( file &&line ) ostr << " ";212 if( line ) ostr << "at line " <<line;223 if( _file ) ostr << "in file '" << *_file << "'"; 224 if( _file && _line ) ostr << " "; 225 if( _line ) ostr << "at line " << _line; 213 226 ostr << ")"; 214 227 } … … 217 230 catch(...) {} 218 231 if( mes ) return mes; 232 return exceptionName(); 233 } 234 235 virtual const char* exceptionName() const { 219 236 return "lemon::DataFormatError"; 220 237 } … … 265 282 catch(...) {} 266 283 if( mes ) return mes; 284 return exceptionName(); 285 } 286 287 virtual const char* exceptionName() const { 267 288 return "lemon::AssertionFailedError"; 268 289 } … … 312 333 313 334 #ifndef LEMON_ASSERT_HANDLER 314 # define LEMON_ASSERT_HANDLER ::lemon::assert_fail 335 # ifdef LEMON_ASSERT_EXCEPTION 336 # define LEMON_ASSERT_HANDLER ::lemon::assert_fail_throw 337 # else 338 # define LEMON_ASSERT_HANDLER ::lemon::assert_fail 339 # endif 315 340 #endif 316 341 … … 323 348 /** 324 349 * \brief Macro for assertions with customizable message 350 * 351 * Macro for assertions with customizable message. 352 * 353 * The behaviour can be customized with LEMON_ASSERT_HANDLER, 354 * LEMON_ASSERT_EXCEPTION and LEMON_ASSERT_ABORT defines. Asserts can be 355 * disabled by defining either NDEBUG or LEMON_DISABLE_ASSERTS macros. 356 * 357 * \todo We should provide some way to reset to the default behaviour, 358 * shouldn't we? 359 * 360 * \todo This whole 'assert' business should be placed in a separate 361 * include file. 325 362 * 326 363 * \todo __PRETTY_FUNCTION__ should be replaced by something … … 334 371 (msg), #exp, LEMON_ASSERT_ABORT), 0))) 335 372 336 # endif // NDEBUG373 #endif // NDEBUG || LEMON_DISABLE_ASSERTS 337 374 338 375 /** -
src/work/klao/error_test.cc
r1067 r1120 16 16 } 17 17 catch(lemon::DataFormatError &e) { 18 e. set_file(fn);19 e. set_line(5);18 e.file(fn); 19 e.line(5); 20 20 throw; 21 21 } … … 28 28 parse_file("input.txt"); 29 29 } 30 catch(lemon::Exception &e) { 31 cerr << "Exception '" << e.exceptionName() 32 << "' caught: " << endl; 33 cerr << e.what() << endl; 34 } 30 35 catch(exception &e) { 31 36 cerr << "Exception caught: " << endl; … … 34 39 35 40 try { 41 throw lemon::LogicError(); 42 } 43 catch(lemon::Exception &e) { 44 cerr << "Exception '" << e.exceptionName() 45 << "' caught: " << endl; 46 cerr << e.what() << endl; 47 } 48 49 try { 36 50 fail_assert(); 51 } 52 catch(lemon::Exception &e) { 53 cerr << "Exception '" << e.exceptionName() 54 << "' caught: " << endl; 55 cerr << e.what() << endl; 37 56 } 38 57 catch(exception &e) { … … 40 59 cerr << e.what() << endl; 41 60 } 61 62 cerr << endl; 42 63 43 64 // assert(1==0); … … 47 68 48 69 #undef LEMON_ASSERT_HANDLER 49 #define LEMON_ASSERT_ HANDLER ::lemon::assert_fail_throw70 #define LEMON_ASSERT_EXCEPTION 50 71 51 72 #include <error.h>
Note: See TracChangeset
for help on using the changeset viewer.