src/work/klao/error.h
changeset 1066 520769d825f2
parent 1056 cbc27743e17a
child 1067 47939f501c81
equal deleted inserted replaced
0:76d4dc5cc9bd 1:dc8e9d17a6ac
    30 namespace lemon {
    30 namespace lemon {
    31 
    31 
    32   /// Exception-safe convenient "error message" class.
    32   /// Exception-safe convenient "error message" class.
    33   class ErrorMessage {
    33   class ErrorMessage {
    34   protected:
    34   protected:
       
    35   ///\e 
    35     boost::shared_ptr<std::ostringstream> buf;
    36     boost::shared_ptr<std::ostringstream> buf;
    36     
    37     
       
    38   ///\e 
    37     bool init() throw() {
    39     bool init() throw() {
    38       try {
    40       try {
    39 	buf.reset(new std::ostringstream);
    41 	buf.reset(new std::ostringstream);
    40       }
    42       }
    41       catch(...) {
    43       catch(...) {
    44       return buf;
    46       return buf;
    45     }
    47     }
    46 
    48 
    47   public:
    49   public:
    48 
    50 
       
    51   ///\e 
    49     ErrorMessage() throw() { init(); }
    52     ErrorMessage() throw() { init(); }
    50 
    53 
       
    54   ///\e 
    51     ErrorMessage(const char *message) throw() {
    55     ErrorMessage(const char *message) throw() {
    52       init();
    56       init();
    53       *this << message;
    57       *this << message;
    54     }
    58     }
    55 
    59 
       
    60   ///\e 
    56     ErrorMessage(const std::string &message) throw() {
    61     ErrorMessage(const std::string &message) throw() {
    57       init();
    62       init();
    58       *this << message;
    63       *this << message;
    59     }
    64     }
    60 
    65 
       
    66   ///\e 
    61     template <typename T>
    67     template <typename T>
    62     ErrorMessage& operator<<(const T &t) throw() {
    68     ErrorMessage& operator<<(const T &t) throw() {
    63       if( !buf ) return *this;
    69       if( !buf ) return *this;
    64 
    70 
    65       try {
    71       try {
    68       catch(...) {
    74       catch(...) {
    69 	buf.reset();
    75 	buf.reset();
    70       }
    76       }
    71     }
    77     }
    72 
    78 
       
    79   ///\e 
    73     const char* message() throw() {
    80     const char* message() throw() {
    74       if( !buf ) return 0;
    81       if( !buf ) return 0;
    75 
    82 
    76       const char* mes = 0;
    83       const char* mes = 0;
    77       try {
    84       try {
    88    *
    95    *
    89    * Base class for exceptions used in LEMON.
    96    * Base class for exceptions used in LEMON.
    90    */
    97    */
    91   class Exception : public std::exception, public ErrorMessage {
    98   class Exception : public std::exception, public ErrorMessage {
    92   public:
    99   public:
       
   100   ///\e 
    93     Exception() throw() {}
   101     Exception() throw() {}
       
   102   ///\e 
    94     explicit Exception(const std::string &s) throw()
   103     explicit Exception(const std::string &s) throw()
    95       : ErrorMessage(s) {}
   104       : ErrorMessage(s) {}
       
   105   ///\e 
    96     virtual ~Exception() throw() {}
   106     virtual ~Exception() throw() {}
    97     
   107     
       
   108   ///\e 
    98     virtual const char* what() const throw() {
   109     virtual const char* what() const throw() {
    99       const char *mes = message();
   110       const char *mes = message();
   100       if( mes ) return mes;
   111       if( mes ) return mes;
   101       return "lemon::Exception";
   112       return "lemon::Exception";
   102     }
   113     }
   103   };
   114   };
   104 
   115 
   105 
   116   ///\e 
   106   class LogicError : public Exception {
   117   class LogicError : public Exception {
       
   118   ///\e 
   107     explicit LogicError(const std::string &s)
   119     explicit LogicError(const std::string &s)
   108       : Exception(s) {}
   120       : Exception(s) {}
   109   };
   121   };
   110 
   122 
       
   123   ///\e 
   111   class RuntimeError : public Exception {
   124   class RuntimeError : public Exception {
       
   125   ///\e 
   112     explicit RuntimeError(const std::string &s)
   126     explicit RuntimeError(const std::string &s)
   113       : Exception(s) {}
   127       : Exception(s) {}
   114   };
   128   };
   115 
   129 
       
   130   ///\e 
   116   class RangeError : public RuntimeError {
   131   class RangeError : public RuntimeError {
       
   132   ///\e 
   117     explicit RangeError(const std::string &s)
   133     explicit RangeError(const std::string &s)
   118       : RuntimeError(s) {}
   134       : RuntimeError(s) {}
   119   };
   135   };
   120 
   136 
       
   137   ///\e 
   121   class IOError : public RuntimeError {
   138   class IOError : public RuntimeError {
       
   139   ///\e 
   122     explicit IOError(const std::string &s)
   140     explicit IOError(const std::string &s)
   123       : RuntimeError(s) {}
   141       : RuntimeError(s) {}
   124   };
   142   };
   125 
   143 
       
   144   ///\e 
   126   class DataFormatError : public IOError {
   145   class DataFormatError : public IOError {
       
   146   ///\e 
   127     explicit DataFormatError(const std::string &message)
   147     explicit DataFormatError(const std::string &message)
   128       : IOError(message) : line(0) {}
   148       : IOError(message) : line(0) {}
       
   149   ///\e 
   129     DataFormatError(const std::string &file_name, int line_num,
   150     DataFormatError(const std::string &file_name, int line_num,
   130 		    sconst std::string &message)
   151 		    const std::string &message)
   131       : IOError(message), line(line_num) { set_file(file_name); }
   152       : IOError(message), line(line_num) { set_file(file_name); }
   132 
   153 
       
   154   ///\e 
   133     void set_line(int line_num) { line=line_num; }
   155     void set_line(int line_num) { line=line_num; }
       
   156   ///\e 
   134     void set_file(const std::string &file_name) {
   157     void set_file(const std::string &file_name) {
   135       try {
   158       try {
   136 	file.reset(new std::string);
   159 	file.reset(new std::string);
   137 	*file = file_name;
   160 	*file = file_name;
   138       }
   161       }
   139       catch(...) {
   162       catch(...) {
   140 	file.reset();
   163 	file.reset();
   141       }
   164       }
   142     }
   165     }
   143 
   166 
       
   167   ///\e 
   144     virtual const char* what() const {
   168     virtual const char* what() const {
   145       const char *mes = 0;
   169       const char *mes = 0;
   146       try {
   170       try {
   147 	std::ostringstream ostr;
   171 	std::ostringstream ostr;
   148 	ostr << IOError::what();
   172 	ostr << IOError::what();