lemon/error.h
branch1.0
changeset 348 a07eb292dd95
parent 290 f6899946c1ac
equal deleted inserted replaced
5:087d88504383 6:c85caade0bba
    39   ///
    39   ///
    40   /// Base class for exceptions used in LEMON.
    40   /// Base class for exceptions used in LEMON.
    41   ///
    41   ///
    42   class Exception : public std::exception {
    42   class Exception : public std::exception {
    43   public:
    43   public:
    44     ///\e Constructor
    44     ///Constructor
    45     Exception() {}
    45     Exception() throw() {}
    46     ///\e Virtual destructor
    46     ///Virtual destructor
    47     virtual ~Exception() throw() {}
    47     virtual ~Exception() throw() {}
    48     ///\e A short description of the exception
    48     ///A short description of the exception
    49     virtual const char* what() const throw() {
    49     virtual const char* what() const throw() {
    50       return "lemon::Exception";
    50       return "lemon::Exception";
    51     }
    51     }
    52   };
    52   };
    53 
    53 
    62 
    62 
    63     mutable std::string _what;
    63     mutable std::string _what;
    64   public:
    64   public:
    65 
    65 
    66     /// Copy constructor
    66     /// Copy constructor
    67     IoError(const IoError &error) {
    67     IoError(const IoError &error) throw() : Exception() {
    68       message(error._message);
    68       message(error._message);
    69       file(error._file);
    69       file(error._file);
    70     }
    70     }
    71 
    71 
    72     /// Constructor
    72     /// Constructor
    73     explicit IoError(const char *message) {
    73     explicit IoError(const char *message) throw() {
    74       IoError::message(message);
    74       IoError::message(message);
    75     }
    75     }
    76 
    76 
    77     /// Constructor
    77     /// Constructor
    78     explicit IoError(const std::string &message) {
    78     explicit IoError(const std::string &message) throw() {
    79       IoError::message(message);
    79       IoError::message(message);
    80     }
    80     }
    81 
    81 
    82     /// Constructor
    82     /// Constructor
    83     IoError(const std::string &file, const char *message) {
    83     explicit IoError(const char *message,
       
    84                      const std::string &file) throw() {
    84       IoError::message(message);
    85       IoError::message(message);
    85       IoError::file(file);
    86       IoError::file(file);
    86     }
    87     }
    87 
    88 
    88     /// Constructor
    89     /// Constructor
    89     IoError(const std::string &file, const std::string &message) {
    90     explicit IoError(const std::string &message,
       
    91                      const std::string &file) throw() {
    90       IoError::message(message);
    92       IoError::message(message);
    91       IoError::file(file);
    93       IoError::file(file);
    92     }
    94     }
    93 
    95 
    94     /// Virtual destructor
    96     /// Virtual destructor
    95     virtual ~IoError() throw() {}
    97     virtual ~IoError() throw() {}
    96 
    98 
    97     /// Set the error message
    99     /// Set the error message
    98     void message(const char *message) {
   100     void message(const char *message) throw() {
    99       try {
   101       try {
   100         _message = message;
   102         _message = message;
   101       } catch (...) {}
   103       } catch (...) {}
   102     }
   104     }
   103 
   105 
   104     /// Set the error message
   106     /// Set the error message
   105     void message(const std::string& message) {
   107     void message(const std::string& message) throw() {
   106       try {
   108       try {
   107         _message = message;
   109         _message = message;
   108       } catch (...) {}
   110       } catch (...) {}
   109     }
   111     }
   110 
   112 
   111     /// Set the file name
   113     /// Set the file name
   112     void file(const std::string &file) {
   114     void file(const std::string &file) throw() {
   113       try {
   115       try {
   114         _file = file;
   116         _file = file;
   115       } catch (...) {}
   117       } catch (...) {}
   116     }
   118     }
   117 
   119 
   118     /// Returns the error message
   120     /// Returns the error message
   119     const std::string& message() const {
   121     const std::string& message() const throw() {
   120       return _message;
   122       return _message;
   121     }
   123     }
   122 
   124 
   123     /// \brief Returns the filename
   125     /// \brief Returns the filename
   124     ///
   126     ///
   125     /// Returns the filename or empty string if the filename was not
   127     /// Returns the filename or an empty string if it was not specified.
   126     /// specified.
   128     const std::string& file() const throw() {
   127     const std::string& file() const {
       
   128       return _file;
   129       return _file;
   129     }
   130     }
   130 
   131 
   131     /// \brief Returns a short error message
   132     /// \brief Returns a short error message
   132     ///
   133     ///
   133     /// Returns a short error message which contains the message, the
   134     /// Returns a short error message which contains the message and the
   134     /// file name and the line number.
   135     /// file name.
   135     virtual const char* what() const throw() {
   136     virtual const char* what() const throw() {
   136       try {
   137       try {
   137         _what.clear();
   138         _what.clear();
   138         std::ostringstream oss;
   139         std::ostringstream oss;
   139         oss << "lemon:IoError" << ": ";
   140         oss << "lemon:IoError" << ": ";
   140         oss << message();
   141         oss << _message;
   141         if (!file().empty()) {
   142         if (!_file.empty()) {
   142           oss << " (";
   143           oss << " ('" << _file << "')";
   143           if (!file().empty()) oss << "with file '" << file() << "'";
       
   144           oss << ")";
       
   145         }
   144         }
   146         _what = oss.str();
   145         _what = oss.str();
   147       }
   146       }
   148       catch (...) {}
   147       catch (...) {}
   149       if (!_what.empty()) return _what.c_str();
   148       if (!_what.empty()) return _what.c_str();
   152 
   151 
   153   };
   152   };
   154 
   153 
   155   /// \brief Format error
   154   /// \brief Format error
   156   ///
   155   ///
   157   /// This class is used to indicate if an input file has wrong
   156   /// This exception is thrown when an input file has wrong
   158   /// formatting, or a data representation is not legal.
   157   /// format or a data representation is not legal.
   159   class FormatError : public Exception {
   158   class FormatError : public Exception {
   160   protected:
   159   protected:
   161     std::string _message;
   160     std::string _message;
   162     std::string _file;
   161     std::string _file;
   163     int _line;
   162     int _line;
   164 
   163 
   165     mutable std::string _what;
   164     mutable std::string _what;
   166   public:
   165   public:
   167 
   166 
   168     /// Copy constructor
   167     /// Copy constructor
   169     FormatError(const FormatError &error) {
   168     FormatError(const FormatError &error) throw() : Exception() {
   170       message(error._message);
   169       message(error._message);
   171       file(error._file);
   170       file(error._file);
   172       line(error._line);
   171       line(error._line);
   173     }
   172     }
   174 
   173 
   175     /// Constructor
   174     /// Constructor
   176     explicit FormatError(const char *message) {
   175     explicit FormatError(const char *message) throw() {
   177       FormatError::message(message);
   176       FormatError::message(message);
   178       _line = 0;
   177       _line = 0;
   179     }
   178     }
   180 
   179 
   181     /// Constructor
   180     /// Constructor
   182     explicit FormatError(const std::string &message) {
   181     explicit FormatError(const std::string &message) throw() {
   183       FormatError::message(message);
   182       FormatError::message(message);
   184       _line = 0;
   183       _line = 0;
   185     }
   184     }
   186 
   185 
   187     /// Constructor
   186     /// Constructor
   188     FormatError(const std::string &file, int line, const char *message) {
   187     explicit FormatError(const char *message,
       
   188                          const std::string &file, int line = 0) throw() {
   189       FormatError::message(message);
   189       FormatError::message(message);
   190       FormatError::file(file);
   190       FormatError::file(file);
   191       FormatError::line(line);
   191       FormatError::line(line);
   192     }
   192     }
   193 
   193 
   194     /// Constructor
   194     /// Constructor
   195     FormatError(const std::string &file, int line, const std::string &message) {
   195     explicit FormatError(const std::string &message,
       
   196                          const std::string &file, int line = 0) throw() {
   196       FormatError::message(message);
   197       FormatError::message(message);
   197       FormatError::file(file);
   198       FormatError::file(file);
   198       FormatError::line(line);
   199       FormatError::line(line);
   199     }
   200     }
   200 
   201 
   201     /// Virtual destructor
   202     /// Virtual destructor
   202     virtual ~FormatError() throw() {}
   203     virtual ~FormatError() throw() {}
   203 
   204 
   204     /// Set the line number
   205     /// Set the line number
   205     void line(int line) { _line = line; }
   206     void line(int line) throw() { _line = line; }
   206 
   207 
   207     /// Set the error message
   208     /// Set the error message
   208     void message(const char *message) {
   209     void message(const char *message) throw() {
   209       try {
   210       try {
   210         _message = message;
   211         _message = message;
   211       } catch (...) {}
   212       } catch (...) {}
   212     }
   213     }
   213 
   214 
   214     /// Set the error message
   215     /// Set the error message
   215     void message(const std::string& message) {
   216     void message(const std::string& message) throw() {
   216       try {
   217       try {
   217         _message = message;
   218         _message = message;
   218       } catch (...) {}
   219       } catch (...) {}
   219     }
   220     }
   220 
   221 
   221     /// Set the file name
   222     /// Set the file name
   222     void file(const std::string &file) {
   223     void file(const std::string &file) throw() {
   223       try {
   224       try {
   224         _file = file;
   225         _file = file;
   225       } catch (...) {}
   226       } catch (...) {}
   226     }
   227     }
   227 
   228 
   228     /// \brief Returns the line number
   229     /// \brief Returns the line number
   229     ///
   230     ///
   230     /// Returns the line number or zero if it was not specified.
   231     /// Returns the line number or zero if it was not specified.
   231     int line() const { return _line; }
   232     int line() const throw() { return _line; }
   232 
   233 
   233     /// Returns the error message
   234     /// Returns the error message
   234     const std::string& message() const {
   235     const std::string& message() const throw() {
   235       return _message;
   236       return _message;
   236     }
   237     }
   237 
   238 
   238     /// \brief Returns the filename
   239     /// \brief Returns the filename
   239     ///
   240     ///
   240     /// Returns the filename or empty string if the filename was not
   241     /// Returns the filename or an empty string if it was not specified.
   241     /// specified.
   242     const std::string& file() const throw() {
   242     const std::string& file() const {
       
   243       return _file;
   243       return _file;
   244     }
   244     }
   245 
   245 
   246     /// \brief Returns a short error message
   246     /// \brief Returns a short error message
   247     ///
   247     ///
   250     virtual const char* what() const throw() {
   250     virtual const char* what() const throw() {
   251       try {
   251       try {
   252         _what.clear();
   252         _what.clear();
   253         std::ostringstream oss;
   253         std::ostringstream oss;
   254         oss << "lemon:FormatError" << ": ";
   254         oss << "lemon:FormatError" << ": ";
   255         oss << message();
   255         oss << _message;
   256         if (!file().empty() || line() != 0) {
   256         if (!_file.empty() || _line != 0) {
   257           oss << " (";
   257           oss << " (";
   258           if (!file().empty()) oss << "in file '" << file() << "'";
   258           if (!_file.empty()) oss << "in file '" << _file << "'";
   259           if (!file().empty() && line() != 0) oss << " ";
   259           if (!_file.empty() && _line != 0) oss << " ";
   260           if (line() != 0) oss << "at line " << line();
   260           if (_line != 0) oss << "at line " << _line;
   261           oss << ")";
   261           oss << ")";
   262         }
   262         }
   263         _what = oss.str();
   263         _what = oss.str();
   264       }
   264       }
   265       catch (...) {}
   265       catch (...) {}