COIN-OR::LEMON - Graph Library

Ticket #17: error_d901321d6555.patch

File error_d901321d6555.patch, 20.5 KB (added by Peter Kovacs, 11 years ago)
  • lemon/error.h

    # HG changeset patch
    # User Peter Kovacs <kpeter@inf.elte.hu>
    # Date 1222855083 -7200
    # Node ID d901321d65558a1fc1017758a73e2a1ecc9df8ad
    # Parent  f6899946c1ac6f8e8a52d278b915afec7ad4568c
    Changing parameter order in exception classes + improvements
    
    diff --git a/lemon/error.h b/lemon/error.h
    a b  
    4141  ///
    4242  class Exception : public std::exception {
    4343  public:
    44     ///\e Constructor
    45     Exception() {}
    46     ///\e Virtual destructor
     44    ///Constructor
     45    Exception() throw() {}
     46    ///Virtual destructor
    4747    virtual ~Exception() throw() {}
    48     ///\e A short description of the exception
     48    ///A short description of the exception
    4949    virtual const char* what() const throw() {
    5050      return "lemon::Exception";
    5151    }
     
    6464  public:
    6565
    6666    /// Copy constructor
    67     IoError(const IoError &error) {
     67    IoError(const IoError &error) throw() : Exception() {
    6868      message(error._message);
    6969      file(error._file);
    7070    }
    7171
    7272    /// Constructor
    73     explicit IoError(const char *message) {
     73    explicit IoError(const char *message) throw() {
    7474      IoError::message(message);
    7575    }
    7676
    7777    /// Constructor
    78     explicit IoError(const std::string &message) {
     78    explicit IoError(const std::string &message) throw() {
    7979      IoError::message(message);
    8080    }
    8181
    8282    /// Constructor
    83     IoError(const std::string &file, const char *message) {
     83    explicit IoError(const char *message,
     84                     const std::string &file) throw() {
    8485      IoError::message(message);
    8586      IoError::file(file);
    8687    }
    8788
    8889    /// Constructor
    89     IoError(const std::string &file, const std::string &message) {
     90    explicit IoError(const std::string &message,
     91                     const std::string &file) throw() {
    9092      IoError::message(message);
    9193      IoError::file(file);
    9294    }
     
    9597    virtual ~IoError() throw() {}
    9698
    9799    /// Set the error message
    98     void message(const char *message) {
     100    void message(const char *message) throw() {
    99101      try {
    100102        _message = message;
    101103      } catch (...) {}
    102104    }
    103105
    104106    /// Set the error message
    105     void message(const std::string& message) {
     107    void message(const std::string& message) throw() {
    106108      try {
    107109        _message = message;
    108110      } catch (...) {}
    109111    }
    110112
    111113    /// Set the file name
    112     void file(const std::string &file) {
     114    void file(const std::string &file) throw() {
    113115      try {
    114116        _file = file;
    115117      } catch (...) {}
    116118    }
    117119
    118120    /// Returns the error message
    119     const std::string& message() const {
     121    const std::string& message() const throw() {
    120122      return _message;
    121123    }
    122124
    123125    /// \brief Returns the filename
    124126    ///
    125     /// Returns the filename or empty string if the filename was not
    126     /// specified.
    127     const std::string& file() const {
     127    /// Returns the filename or an empty string if it was not specified.
     128    const std::string& file() const throw() {
    128129      return _file;
    129130    }
    130131
    131132    /// \brief Returns a short error message
    132133    ///
    133     /// Returns a short error message which contains the message, the
    134     /// file name and the line number.
     134    /// Returns a short error message which contains the message and the
     135    /// file name.
    135136    virtual const char* what() const throw() {
    136137      try {
    137138        _what.clear();
    138139        std::ostringstream oss;
    139140        oss << "lemon:IoError" << ": ";
    140         oss << message();
    141         if (!file().empty()) {
    142           oss << " (";
    143           if (!file().empty()) oss << "with file '" << file() << "'";
    144           oss << ")";
     141        oss << _message;
     142        if (!_file.empty()) {
     143          oss << " ('" << _file << "')";
    145144        }
    146145        _what = oss.str();
    147146      }
     
    154153
    155154  /// \brief Format error
    156155  ///
    157   /// This class is used to indicate if an input file has wrong
    158   /// formatting, or a data representation is not legal.
     156  /// This exception is thrown when an input file has wrong
     157  /// format or a data representation is not legal.
    159158  class FormatError : public Exception {
    160159  protected:
    161160    std::string _message;
     
    166165  public:
    167166
    168167    /// Copy constructor
    169     FormatError(const FormatError &error) {
     168    FormatError(const FormatError &error) throw() : Exception() {
    170169      message(error._message);
    171170      file(error._file);
    172171      line(error._line);
    173172    }
    174173
    175174    /// Constructor
    176     explicit FormatError(const char *message) {
     175    explicit FormatError(const char *message) throw() {
    177176      FormatError::message(message);
    178177      _line = 0;
    179178    }
    180179
    181180    /// Constructor
    182     explicit FormatError(const std::string &message) {
     181    explicit FormatError(const std::string &message) throw() {
    183182      FormatError::message(message);
    184183      _line = 0;
    185184    }
    186185
    187186    /// 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() {
    189189      FormatError::message(message);
    190190      FormatError::file(file);
    191191      FormatError::line(line);
    192192    }
    193193
    194194    /// 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() {
    196197      FormatError::message(message);
    197198      FormatError::file(file);
    198199      FormatError::line(line);
     
    202203    virtual ~FormatError() throw() {}
    203204
    204205    /// Set the line number
    205     void line(int line) { _line = line; }
     206    void line(int line) throw() { _line = line; }
    206207
    207208    /// Set the error message
    208     void message(const char *message) {
     209    void message(const char *message) throw() {
    209210      try {
    210211        _message = message;
    211212      } catch (...) {}
    212213    }
    213214
    214215    /// Set the error message
    215     void message(const std::string& message) {
     216    void message(const std::string& message) throw() {
    216217      try {
    217218        _message = message;
    218219      } catch (...) {}
    219220    }
    220221
    221222    /// Set the file name
    222     void file(const std::string &file) {
     223    void file(const std::string &file) throw() {
    223224      try {
    224225        _file = file;
    225226      } catch (...) {}
     
    228229    /// \brief Returns the line number
    229230    ///
    230231    /// Returns the line number or zero if it was not specified.
    231     int line() const { return _line; }
     232    int line() const throw() { return _line; }
    232233
    233234    /// Returns the error message
    234     const std::string& message() const {
     235    const std::string& message() const throw() {
    235236      return _message;
    236237    }
    237238
    238239    /// \brief Returns the filename
    239240    ///
    240     /// Returns the filename or empty string if the filename was not
    241     /// specified.
    242     const std::string& file() const {
     241    /// Returns the filename or an empty string if it was not specified.
     242    const std::string& file() const throw() {
    243243      return _file;
    244244    }
    245245
     
    252252        _what.clear();
    253253        std::ostringstream oss;
    254254        oss << "lemon:FormatError" << ": ";
    255         oss << message();
    256         if (!file().empty() || line() != 0) {
     255        oss << _message;
     256        if (!_file.empty() || _line != 0) {
    257257          oss << " (";
    258           if (!file().empty()) oss << "in file '" << file() << "'";
    259           if (!file().empty() && line() != 0) oss << " ";
    260           if (line() != 0) oss << "at line " << line();
     258          if (!_file.empty()) oss << "in file '" << _file << "'";
     259          if (!_file.empty() && _line != 0) oss << " ";
     260          if (_line != 0) oss << "at line " << _line;
    261261          oss << ")";
    262262        }
    263263        _what = oss.str();
  • lemon/graph_to_eps.h

    diff --git a/lemon/graph_to_eps.h b/lemon/graph_to_eps.h
    a b  
    11701170  std::ostream* os = new std::ofstream(file_name);
    11711171  if (!(*os)) {
    11721172    delete os;
    1173     throw IoError(file_name, "Cannot write file");
     1173    throw IoError("Cannot write file", file_name);
    11741174  }
    11751175  return GraphToEps<DefaultGraphToEpsTraits<G> >
    11761176    (DefaultGraphToEpsTraits<G>(g,*os,true));
     
    11911191  std::ostream* os = new std::ofstream(file_name.c_str());
    11921192  if (!(*os)) {
    11931193    delete os;
    1194     throw IoError(file_name, "Cannot write file");
     1194    throw IoError("Cannot write file", file_name);
    11951195  }
    11961196  return GraphToEps<DefaultGraphToEpsTraits<G> >
    11971197    (DefaultGraphToEpsTraits<G>(g,*os,true));
  • lemon/lgf_reader.h

    diff --git a/lemon/lgf_reader.h b/lemon/lgf_reader.h
    a b  
    516516        _filename(fn), _digraph(digraph),
    517517        _use_nodes(false), _use_arcs(false),
    518518        _skip_nodes(false), _skip_arcs(false) {
    519       if (!(*_is)) throw IoError(fn, "Cannot open file");
     519      if (!(*_is)) throw IoError("Cannot open file", fn);
    520520    }
    521521
    522522    /// \brief Constructor
     
    528528        _filename(fn), _digraph(digraph),
    529529        _use_nodes(false), _use_arcs(false),
    530530        _skip_nodes(false), _skip_arcs(false) {
    531       if (!(*_is)) throw IoError(fn, "Cannot open file");
     531      if (!(*_is)) throw IoError("Cannot open file", fn);
    532532    }
    533533
    534534    /// \brief Destructor
     
    879879            maps.find(_node_maps[i].first);
    880880          if (jt == maps.end()) {
    881881            std::ostringstream msg;
    882             msg << "Map not found in file: " << _node_maps[i].first;
     882            msg << "Map not found: " << _node_maps[i].first;
    883883            throw FormatError(msg.str());
    884884          }
    885885          map_index[i] = jt->second;
     
    908908          }
    909909        }
    910910        if (line >> std::ws >> c)
    911           throw FormatError("Extra character on the end of line");
     911          throw FormatError("Extra character at the end of line");
    912912
    913913        Node n;
    914914        if (!_use_nodes) {
     
    917917            _node_index.insert(std::make_pair(tokens[label_index], n));
    918918        } else {
    919919          if (label_index == -1)
    920             throw FormatError("Label map not found in file");
     920            throw FormatError("Label map not found");
    921921          typename std::map<std::string, Node>::iterator it =
    922922            _node_index.find(tokens[label_index]);
    923923          if (it == _node_index.end()) {
     
    972972            maps.find(_arc_maps[i].first);
    973973          if (jt == maps.end()) {
    974974            std::ostringstream msg;
    975             msg << "Map not found in file: " << _arc_maps[i].first;
     975            msg << "Map not found: " << _arc_maps[i].first;
    976976            throw FormatError(msg.str());
    977977          }
    978978          map_index[i] = jt->second;
     
    10101010          }
    10111011        }
    10121012        if (line >> std::ws >> c)
    1013           throw FormatError("Extra character on the end of line");
     1013          throw FormatError("Extra character at the end of line");
    10141014
    10151015        Arc a;
    10161016        if (!_use_arcs) {
     
    10381038            _arc_index.insert(std::make_pair(tokens[label_index], a));
    10391039        } else {
    10401040          if (label_index == -1)
    1041             throw FormatError("Label map not found in file");
     1041            throw FormatError("Label map not found");
    10421042          typename std::map<std::string, Arc>::iterator it =
    10431043            _arc_index.find(tokens[label_index]);
    10441044          if (it == _arc_index.end()) {
     
    10731073        if (!_reader_bits::readToken(line, token))
    10741074          throw FormatError("Attribute value not found");
    10751075        if (line >> c)
    1076           throw FormatError("Extra character on the end of line");
     1076          throw FormatError("Extra character at the end of line");
    10771077
    10781078        {
    10791079          std::set<std::string>::iterator it = read_attr.find(attr);
    10801080          if (it != read_attr.end()) {
    10811081            std::ostringstream msg;
    1082             msg << "Multiple occurence of attribute " << attr;
     1082            msg << "Multiple occurence of attribute: " << attr;
    10831083            throw FormatError(msg.str());
    10841084          }
    10851085          read_attr.insert(attr);
     
    11011101           it != _attributes.end(); ++it) {
    11021102        if (read_attr.find(it->first) == read_attr.end()) {
    11031103          std::ostringstream msg;
    1104           msg << "Attribute not found in file: " << it->first;
     1104          msg << "Attribute not found: " << it->first;
    11051105          throw FormatError(msg.str());
    11061106        }
    11071107      }
     
    11171117    /// This function starts the batch processing
    11181118    void run() {
    11191119      LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
    1120       if (!*_is) {
    1121         throw FormatError("Cannot find file");
    1122       }
    11231120
    11241121      bool nodes_done = _skip_nodes;
    11251122      bool arcs_done = _skip_arcs;
     
    11381135          _reader_bits::readToken(line, caption);
    11391136
    11401137          if (line >> c)
    1141             throw FormatError("Extra character on the end of line");
     1138            throw FormatError("Extra character at the end of line");
    11421139
    11431140          if (section == "nodes" && !nodes_done) {
    11441141            if (_nodes_caption.empty() || _nodes_caption == caption) {
     
    13081305        _filename(fn), _graph(graph),
    13091306        _use_nodes(false), _use_edges(false),
    13101307        _skip_nodes(false), _skip_edges(false) {
    1311       if (!(*_is)) throw IoError(fn, "Cannot open file");
     1308      if (!(*_is)) throw IoError("Cannot open file", fn);
    13121309    }
    13131310
    13141311    /// \brief Constructor
     
    13201317        _filename(fn), _graph(graph),
    13211318        _use_nodes(false), _use_edges(false),
    13221319        _skip_nodes(false), _skip_edges(false) {
    1323       if (!(*_is)) throw IoError(fn, "Cannot open file");
     1320      if (!(*_is)) throw IoError("Cannot open file", fn);
    13241321    }
    13251322
    13261323    /// \brief Destructor
     
    17151712            maps.find(_node_maps[i].first);
    17161713          if (jt == maps.end()) {
    17171714            std::ostringstream msg;
    1718             msg << "Map not found in file: " << _node_maps[i].first;
     1715            msg << "Map not found: " << _node_maps[i].first;
    17191716            throw FormatError(msg.str());
    17201717          }
    17211718          map_index[i] = jt->second;
     
    17441741          }
    17451742        }
    17461743        if (line >> std::ws >> c)
    1747           throw FormatError("Extra character on the end of line");
     1744          throw FormatError("Extra character at the end of line");
    17481745
    17491746        Node n;
    17501747        if (!_use_nodes) {
     
    17531750            _node_index.insert(std::make_pair(tokens[label_index], n));
    17541751        } else {
    17551752          if (label_index == -1)
    1756             throw FormatError("Label map not found in file");
     1753            throw FormatError("Label map not found");
    17571754          typename std::map<std::string, Node>::iterator it =
    17581755            _node_index.find(tokens[label_index]);
    17591756          if (it == _node_index.end()) {
     
    18081805            maps.find(_edge_maps[i].first);
    18091806          if (jt == maps.end()) {
    18101807            std::ostringstream msg;
    1811             msg << "Map not found in file: " << _edge_maps[i].first;
     1808            msg << "Map not found: " << _edge_maps[i].first;
    18121809            throw FormatError(msg.str());
    18131810          }
    18141811          map_index[i] = jt->second;
     
    18461843          }
    18471844        }
    18481845        if (line >> std::ws >> c)
    1849           throw FormatError("Extra character on the end of line");
     1846          throw FormatError("Extra character at the end of line");
    18501847
    18511848        Edge e;
    18521849        if (!_use_edges) {
     
    18741871            _edge_index.insert(std::make_pair(tokens[label_index], e));
    18751872        } else {
    18761873          if (label_index == -1)
    1877             throw FormatError("Label map not found in file");
     1874            throw FormatError("Label map not found");
    18781875          typename std::map<std::string, Edge>::iterator it =
    18791876            _edge_index.find(tokens[label_index]);
    18801877          if (it == _edge_index.end()) {
     
    19091906        if (!_reader_bits::readToken(line, token))
    19101907          throw FormatError("Attribute value not found");
    19111908        if (line >> c)
    1912           throw FormatError("Extra character on the end of line");
     1909          throw FormatError("Extra character at the end of line");
    19131910
    19141911        {
    19151912          std::set<std::string>::iterator it = read_attr.find(attr);
    19161913          if (it != read_attr.end()) {
    19171914            std::ostringstream msg;
    1918             msg << "Multiple occurence of attribute " << attr;
     1915            msg << "Multiple occurence of attribute: " << attr;
    19191916            throw FormatError(msg.str());
    19201917          }
    19211918          read_attr.insert(attr);
     
    19371934           it != _attributes.end(); ++it) {
    19381935        if (read_attr.find(it->first) == read_attr.end()) {
    19391936          std::ostringstream msg;
    1940           msg << "Attribute not found in file: " << it->first;
     1937          msg << "Attribute not found: " << it->first;
    19411938          throw FormatError(msg.str());
    19421939        }
    19431940      }
     
    19721969          _reader_bits::readToken(line, caption);
    19731970
    19741971          if (line >> c)
    1975             throw FormatError("Extra character on the end of line");
     1972            throw FormatError("Extra character at the end of line");
    19761973
    19771974          if (section == "nodes" && !nodes_done) {
    19781975            if (_nodes_caption.empty() || _nodes_caption == caption) {
     
    20952092    SectionReader(const std::string& fn)
    20962093      : _is(new std::ifstream(fn.c_str())), local_is(true),
    20972094        _filename(fn) {
    2098       if (!(*_is)) throw IoError(fn, "Cannot open file");
     2095      if (!(*_is)) throw IoError("Cannot open file", fn);
    20992096    }
    21002097
    21012098    /// \brief Constructor
     
    21042101    SectionReader(const char* fn)
    21052102      : _is(new std::ifstream(fn)), local_is(true),
    21062103        _filename(fn) {
    2107       if (!(*_is)) throw IoError(fn, "Cannot open file");
     2104      if (!(*_is)) throw IoError("Cannot open file", fn);
    21082105    }
    21092106
    21102107    /// \brief Destructor
     
    22612258          _reader_bits::readToken(line, caption);
    22622259
    22632260          if (line >> c)
    2264             throw FormatError("Extra character on the end of line");
     2261            throw FormatError("Extra character at the end of line");
    22652262
    22662263          if (extra_sections.find(section) != extra_sections.end()) {
    22672264            std::ostringstream msg;
    2268             msg << "Multiple occurence of section " << section;
     2265            msg << "Multiple occurence of section: " << section;
    22692266            throw FormatError(msg.str());
    22702267          }
    22712268          Sections::iterator it = _sections.find(section);
     
    23872384    /// file.
    23882385    LgfContents(const std::string& fn)
    23892386      : _is(new std::ifstream(fn.c_str())), local_is(true) {
    2390       if (!(*_is)) throw IoError(fn, "Cannot open file");
     2387      if (!(*_is)) throw IoError("Cannot open file", fn);
    23912388    }
    23922389
    23932390    /// \brief Constructor
     
    23962393    /// file.
    23972394    LgfContents(const char* fn)
    23982395      : _is(new std::ifstream(fn)), local_is(true) {
    2399       if (!(*_is)) throw IoError(fn, "Cannot open file");
     2396      if (!(*_is)) throw IoError("Cannot open file", fn);
    24002397    }
    24012398
    24022399    /// \brief Destructor
  • lemon/lgf_writer.h

    diff --git a/lemon/lgf_writer.h b/lemon/lgf_writer.h
    a b  
    463463    DigraphWriter(const std::string& fn, const Digraph& digraph)
    464464      : _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph),
    465465        _skip_nodes(false), _skip_arcs(false) {
    466       if (!(*_os)) throw IoError(fn, "Cannot write file");
     466      if (!(*_os)) throw IoError("Cannot write file", fn);
    467467    }
    468468
    469469    /// \brief Constructor
     
    473473    DigraphWriter(const char* fn, const Digraph& digraph)
    474474      : _os(new std::ofstream(fn)), local_os(true), _digraph(digraph),
    475475        _skip_nodes(false), _skip_arcs(false) {
    476       if (!(*_os)) throw IoError(fn, "Cannot write file");
     476      if (!(*_os)) throw IoError("Cannot write file", fn);
    477477    }
    478478
    479479    /// \brief Destructor
     
    10231023    GraphWriter(const std::string& fn, const Graph& graph)
    10241024      : _os(new std::ofstream(fn.c_str())), local_os(true), _graph(graph),
    10251025        _skip_nodes(false), _skip_edges(false) {
    1026       if (!(*_os)) throw IoError(fn, "Cannot write file");
     1026      if (!(*_os)) throw IoError("Cannot write file", fn);
    10271027    }
    10281028
    10291029    /// \brief Constructor
     
    10331033    GraphWriter(const char* fn, const Graph& graph)
    10341034      : _os(new std::ofstream(fn)), local_os(true), _graph(graph),
    10351035        _skip_nodes(false), _skip_edges(false) {
    1036       if (!(*_os)) throw IoError(fn, "Cannot write file");
     1036      if (!(*_os)) throw IoError("Cannot write file", fn);
    10371037    }
    10381038
    10391039    /// \brief Destructor
     
    15851585    /// Construct a section writer, which writes into the given file.
    15861586    SectionWriter(const std::string& fn)
    15871587      : _os(new std::ofstream(fn.c_str())), local_os(true) {
    1588       if (!(*_os)) throw IoError(fn, "Cannot write file");
     1588      if (!(*_os)) throw IoError("Cannot write file", fn);
    15891589    }
    15901590
    15911591    /// \brief Constructor
     
    15931593    /// Construct a section writer, which writes into the given file.
    15941594    SectionWriter(const char* fn)
    15951595      : _os(new std::ofstream(fn)), local_os(true) {
    1596       if (!(*_os)) throw IoError(fn, "Cannot write file");
     1596      if (!(*_os)) throw IoError("Cannot write file", fn);
    15971597    }
    15981598
    15991599    /// \brief Destructor