COIN-OR::LEMON - Graph Library

Changeset 1120:5d8d64bde9c5 in lemon-0.x for src


Ignore:
Timestamp:
02/03/05 20:24:42 (19 years ago)
Author:
Mihaly Barasz
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1519
Message:

Latest LEMON exception and assert concepts

Location:
src/work/klao
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/work/klao/error.h

    r1068 r1120  
    103103   */
    104104  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() {}
    115108    ///\e
    116109    virtual ~Exception() throw() {}
     110
     111    ///\e
     112    virtual const char* exceptionName() const {
     113      return "lemon::Exception";
     114    }
    117115   
    118116    ///\e
    119117    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   */
    126131  class LogicError : public Exception {
    127132  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   */
    136146  class RuntimeError : public Exception {
    137147  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    }
    143151  };
    144152
     
    146154  class RangeError : public RuntimeError {
    147155  public:
    148     ///\e
    149     explicit RangeError(const char *s)
    150       : RuntimeError(s) {}
     156    virtual const char* exceptionName() const {
     157      return "lemon::RangeError";
     158    }
    151159  };
    152160
     
    154162  class IOError : public RuntimeError {
    155163  public:
    156     ///\e
    157     explicit IOError(const char *s)
    158       : RuntimeError(s) {}
     164    virtual const char* exceptionName() const {
     165      return "lemon::IOError";
     166    }
    159167  };
    160168
     
    162170  class DataFormatError : public IOError {
    163171  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) {}
    171180    ///\e
    172181    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;
    183194      }
    184195      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; }
    191204
    192205    /// \brief Returns the filename.
    193206    ///
    194207    /// 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();
    198211      else
    199212        return "(unknown)";
     
    205218      try {
    206219        std::ostringstream ostr;
    207         ostr << IOError::what();
    208         if( file || line ) {
     220        ostr << _message;
     221        if( _file || _line ) {
    209222          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;
    213226          ostr << ")";
    214227        }
     
    217230      catch(...) {}
    218231      if( mes ) return mes;
     232      return exceptionName();
     233    }
     234
     235    virtual const char* exceptionName() const {
    219236      return "lemon::DataFormatError";
    220237    }
     
    265282      catch(...) {}
    266283      if( mes ) return mes;
     284      return exceptionName();
     285    }
     286
     287    virtual const char* exceptionName() const {
    267288      return "lemon::AssertionFailedError";
    268289    }
     
    312333
    313334#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
    315340#endif
    316341
     
    323348/**
    324349 * \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.
    325362 *
    326363 * \todo __PRETTY_FUNCTION__ should be replaced by something
     
    334371                            (msg), #exp, LEMON_ASSERT_ABORT), 0)))
    335372
    336 # endif // NDEBUG
     373#endif // NDEBUG || LEMON_DISABLE_ASSERTS
    337374
    338375/**
  • src/work/klao/error_test.cc

    r1067 r1120  
    1616  }
    1717  catch(lemon::DataFormatError &e) {
    18     e.set_file(fn);
    19     e.set_line(5);
     18    e.file(fn);
     19    e.line(5);
    2020    throw;
    2121  }
     
    2828    parse_file("input.txt");
    2929  }
     30  catch(lemon::Exception &e) {
     31    cerr << "Exception '" << e.exceptionName()
     32         << "' caught: " << endl;
     33    cerr << e.what() << endl;
     34  }
    3035  catch(exception &e) {
    3136    cerr << "Exception caught: " << endl;
     
    3439
    3540  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 {
    3650    fail_assert();
     51  }
     52  catch(lemon::Exception &e) {
     53    cerr << "Exception '" << e.exceptionName()
     54         << "' caught: " << endl;
     55    cerr << e.what() << endl;
    3756  }
    3857  catch(exception &e) {
     
    4059    cerr << e.what() << endl;
    4160  }
     61
     62  cerr << endl;
    4263
    4364  // assert(1==0);
     
    4768
    4869#undef LEMON_ASSERT_HANDLER
    49 #define LEMON_ASSERT_HANDLER ::lemon::assert_fail_throw
     70#define LEMON_ASSERT_EXCEPTION
    5071
    5172#include <error.h>
Note: See TracChangeset for help on using the changeset viewer.