lemon/error.h
changeset 2151 38ec4a930c05
parent 2150 cce8ac91c08c
child 2153 b1fb96088350
equal deleted inserted replaced
8:6439bd221bc0 9:7b8b3ed0dda4
   174   public:
   174   public:
   175     ///\e 
   175     ///\e 
   176     Exception() {}
   176     Exception() {}
   177     ///\e 
   177     ///\e 
   178     virtual ~Exception() throw() {}
   178     virtual ~Exception() throw() {}
   179 
   179     ///\e 
   180     ///\e
   180     virtual const char* what() const throw() {
   181     virtual const char* exceptionName() const {
       
   182       return "lemon::Exception";
   181       return "lemon::Exception";
   183     }
       
   184     
       
   185     ///\e 
       
   186     virtual const char* what() const throw() {
       
   187       return exceptionName();
       
   188     }
   182     }
   189   };
   183   };
   190 
   184 
   191   /**
   185   /**
   192    * \brief One of the two main subclasses of \ref Exception.
   186    * \brief One of the two main subclasses of \ref Exception.
   197    *
   191    *
   198    * A typical example for this is \ref UninitializedParameter.
   192    * A typical example for this is \ref UninitializedParameter.
   199    */
   193    */
   200   class LogicError : public Exception {
   194   class LogicError : public Exception {
   201   public:
   195   public:
   202     virtual const char* exceptionName() const {
   196     virtual const char* what() const throw() {
   203       return "lemon::LogicError";
   197       return "lemon::LogicError";
   204     }
   198     }
   205   };
   199   };
   206 
   200 
   207   /**
   201   /**
   210    * This error represents problems in the initialization
   204    * This error represents problems in the initialization
   211    * of the parameters of the algorithms.
   205    * of the parameters of the algorithms.
   212    */
   206    */
   213   class UninitializedParameter : public LogicError {
   207   class UninitializedParameter : public LogicError {
   214   public:
   208   public:
   215     virtual const char* exceptionName() const {
   209     virtual const char* what() const throw() {
   216       return "lemon::UninitializedParameter";
   210       return "lemon::UninitializedParameter";
   217     }
   211     }
   218   };
   212   };
   219 
   213 
   220   
   214   
   225    * they cannot be easily predicted and can generally only be caught as
   219    * they cannot be easily predicted and can generally only be caught as
   226    * the program executes.
   220    * the program executes.
   227    */
   221    */
   228   class RuntimeError : public Exception {
   222   class RuntimeError : public Exception {
   229   public:
   223   public:
   230     virtual const char* exceptionName() const {
   224     virtual const char* what() const throw() {
   231       return "lemon::RuntimeError";
   225       return "lemon::RuntimeError";
   232     }
   226     }
   233   };
   227   };
   234 
   228 
   235   ///\e
   229   ///\e
   236   class RangeError : public RuntimeError {
   230   class RangeError : public RuntimeError {
   237   public:
   231   public:
   238     virtual const char* exceptionName() const {
   232     virtual const char* what() const throw() {
   239       return "lemon::RangeError";
   233       return "lemon::RangeError";
   240     }
   234     }
   241   };
   235   };
   242 
   236 
   243   ///\e 
   237   ///\e 
   244   class IOError : public RuntimeError {
   238   class IOError : public RuntimeError {
   245   public:
   239   public:
   246     virtual const char* exceptionName() const {
   240     virtual const char* what() const throw() {
   247       return "lemon::IOError";
   241       return "lemon::IOError";
   248     }
   242     }
   249   };
   243   };
   250 
   244 
   251   ///\e 
   245   ///\e 
   302 
   296 
   303     ///\e 
   297     ///\e 
   304     virtual const char* what() const throw() {
   298     virtual const char* what() const throw() {
   305       try {
   299       try {
   306 	std::ostringstream ostr;
   300 	std::ostringstream ostr;
   307 	ostr << exceptionName() << ": ";
   301 	ostr << "lemon:DataFormatError" << ": ";
   308 	if (message()) ostr << message();
   302 	if (message()) ostr << message();
   309 	if( file() || line() != 0 ) {
   303 	if( file() || line() != 0 ) {
   310 	  ostr << " (";
   304 	  ostr << " (";
   311 	  if( file() ) ostr << "in file '" << file() << "'";
   305 	  if( file() ) ostr << "in file '" << file() << "'";
   312 	  if( file() && line() != 0 ) ostr << " ";
   306 	  if( file() && line() != 0 ) ostr << " ";
   315 	}
   309 	}
   316 	_message_holder.set(ostr.str());
   310 	_message_holder.set(ostr.str());
   317       }
   311       }
   318       catch (...) {}
   312       catch (...) {}
   319       if( _message_holder.valid()) return _message_holder.get().c_str();
   313       if( _message_holder.valid()) return _message_holder.get().c_str();
   320       return exceptionName();
   314       return "lemon:DataFormatError";
   321     }
       
   322 
       
   323     virtual const char* exceptionName() const {
       
   324       return "lemon::DataFormatError";
       
   325     }
   315     }
   326 
   316 
   327     virtual ~DataFormatError() throw() {}
   317     virtual ~DataFormatError() throw() {}
   328   };
   318   };
   329 
   319 
   359 
   349 
   360     ///\e 
   350     ///\e 
   361     virtual const char* what() const throw() {
   351     virtual const char* what() const throw() {
   362       try {
   352       try {
   363 	std::ostringstream ostr;
   353 	std::ostringstream ostr;
   364 	ostr << exceptionName() << ": ";
   354 	ostr << "lemon::FileOpenError" << ": ";
   365 	ostr << "Cannot open file - " << file();
   355 	ostr << "Cannot open file - " << file();
   366 	_message_holder.set(ostr.str());
   356 	_message_holder.set(ostr.str());
   367       }
   357       }
   368       catch (...) {}
   358       catch (...) {}
   369       if( _message_holder.valid()) return _message_holder.get().c_str();
   359       if( _message_holder.valid()) return _message_holder.get().c_str();
   370       return exceptionName();
       
   371     }
       
   372 
       
   373     virtual const char* exceptionName() const {
       
   374       return "lemon::FileOpenError";
   360       return "lemon::FileOpenError";
   375     }
   361     }
   376 
       
   377     virtual ~FileOpenError() throw() {}
   362     virtual ~FileOpenError() throw() {}
   378   };
   363   };
   379 
   364 
   380   class IOParameterError : public IOError {
   365   class IOParameterError : public IOError {
   381   protected:
   366   protected:
   429 	if (file()) ostr << "(when reading file '" << file() << "')";
   414 	if (file()) ostr << "(when reading file '" << file() << "')";
   430 	_message_holder.set(ostr.str());
   415 	_message_holder.set(ostr.str());
   431       }
   416       }
   432       catch (...) {}
   417       catch (...) {}
   433       if( _message_holder.valid() ) return _message_holder.get().c_str();
   418       if( _message_holder.valid() ) return _message_holder.get().c_str();
   434       return exceptionName();
   419       return "lemon:IOParameterError";
   435     }
   420     }
   436 
       
   437     virtual const char* exceptionName() const {
       
   438       return "lemon::IOParameterError";
       
   439     }
       
   440 
       
   441     virtual ~IOParameterError() throw() {}
   421     virtual ~IOParameterError() throw() {}
   442   };
   422   };
   443 
   423 
   444 
   424 
   445   ///\e
   425   ///\e
   483 	_message_holder.set(ostr.str());
   463 	_message_holder.set(ostr.str());
   484 	return ostr.str().c_str();
   464 	return ostr.str().c_str();
   485       }
   465       }
   486       catch(...) {}
   466       catch(...) {}
   487       if( _message_holder.valid() ) return _message_holder.get().c_str();
   467       if( _message_holder.valid() ) return _message_holder.get().c_str();
   488       return exceptionName();
       
   489     }
       
   490 
       
   491     virtual const char* exceptionName() const {
       
   492       return "lemon::AssertionFailedError";
   468       return "lemon::AssertionFailedError";
   493     }
   469     }
   494 
   470    virtual ~AssertionFailedError() throw() {}
   495     virtual ~AssertionFailedError() throw() {}
       
   496   };
   471   };
   497 
   472 
   498 
   473 
   499   /****************  Macros  ****************/
   474   /****************  Macros  ****************/
   500 
   475