diff --git a/lemon/error.h b/lemon/error.h --- a/lemon/error.h +++ b/lemon/error.h @@ -41,11 +41,11 @@ /// class Exception : public std::exception { public: - ///\e Constructor - Exception() {} - ///\e Virtual destructor + ///Constructor + Exception() throw() {} + ///Virtual destructor virtual ~Exception() throw() {} - ///\e A short description of the exception + ///A short description of the exception virtual const char* what() const throw() { return "lemon::Exception"; } @@ -64,29 +64,31 @@ public: /// Copy constructor - IoError(const IoError &error) { + IoError(const IoError &error) throw() : Exception() { message(error._message); file(error._file); } /// Constructor - explicit IoError(const char *message) { + explicit IoError(const char *message) throw() { IoError::message(message); } /// Constructor - explicit IoError(const std::string &message) { + explicit IoError(const std::string &message) throw() { IoError::message(message); } /// Constructor - IoError(const std::string &file, const char *message) { + explicit IoError(const char *message, + const std::string &file) throw() { IoError::message(message); IoError::file(file); } /// Constructor - IoError(const std::string &file, const std::string &message) { + explicit IoError(const std::string &message, + const std::string &file) throw() { IoError::message(message); IoError::file(file); } @@ -95,53 +97,50 @@ virtual ~IoError() throw() {} /// Set the error message - void message(const char *message) { + void message(const char *message) throw() { try { _message = message; } catch (...) {} } /// Set the error message - void message(const std::string& message) { + void message(const std::string& message) throw() { try { _message = message; } catch (...) {} } /// Set the file name - void file(const std::string &file) { + void file(const std::string &file) throw() { try { _file = file; } catch (...) {} } /// Returns the error message - const std::string& message() const { + const std::string& message() const throw() { return _message; } /// \brief Returns the filename /// - /// Returns the filename or empty string if the filename was not - /// specified. - const std::string& file() const { + /// Returns the filename or an empty string if it was not specified. + const std::string& file() const throw() { return _file; } /// \brief Returns a short error message /// - /// Returns a short error message which contains the message, the - /// file name and the line number. + /// Returns a short error message which contains the message and the + /// file name. virtual const char* what() const throw() { try { _what.clear(); std::ostringstream oss; oss << "lemon:IoError" << ": "; - oss << message(); - if (!file().empty()) { - oss << " ("; - if (!file().empty()) oss << "with file '" << file() << "'"; - oss << ")"; + oss << _message; + if (!_file.empty()) { + oss << " ('" << _file << "')"; } _what = oss.str(); } @@ -154,8 +153,8 @@ /// \brief Format error /// - /// This class is used to indicate if an input file has wrong - /// formatting, or a data representation is not legal. + /// This exception is thrown when an input file has wrong + /// format or a data representation is not legal. class FormatError : public Exception { protected: std::string _message; @@ -166,33 +165,35 @@ public: /// Copy constructor - FormatError(const FormatError &error) { + FormatError(const FormatError &error) throw() : Exception() { message(error._message); file(error._file); line(error._line); } /// Constructor - explicit FormatError(const char *message) { + explicit FormatError(const char *message) throw() { FormatError::message(message); _line = 0; } /// Constructor - explicit FormatError(const std::string &message) { + explicit FormatError(const std::string &message) throw() { FormatError::message(message); _line = 0; } /// Constructor - FormatError(const std::string &file, int line, const char *message) { + explicit FormatError(const char *message, + const std::string &file, int line = 0) throw() { FormatError::message(message); FormatError::file(file); FormatError::line(line); } /// Constructor - FormatError(const std::string &file, int line, const std::string &message) { + explicit FormatError(const std::string &message, + const std::string &file, int line = 0) throw() { FormatError::message(message); FormatError::file(file); FormatError::line(line); @@ -202,24 +203,24 @@ virtual ~FormatError() throw() {} /// Set the line number - void line(int line) { _line = line; } + void line(int line) throw() { _line = line; } /// Set the error message - void message(const char *message) { + void message(const char *message) throw() { try { _message = message; } catch (...) {} } /// Set the error message - void message(const std::string& message) { + void message(const std::string& message) throw() { try { _message = message; } catch (...) {} } /// Set the file name - void file(const std::string &file) { + void file(const std::string &file) throw() { try { _file = file; } catch (...) {} @@ -228,18 +229,17 @@ /// \brief Returns the line number /// /// Returns the line number or zero if it was not specified. - int line() const { return _line; } + int line() const throw() { return _line; } /// Returns the error message - const std::string& message() const { + const std::string& message() const throw() { return _message; } /// \brief Returns the filename /// - /// Returns the filename or empty string if the filename was not - /// specified. - const std::string& file() const { + /// Returns the filename or an empty string if it was not specified. + const std::string& file() const throw() { return _file; } @@ -252,12 +252,12 @@ _what.clear(); std::ostringstream oss; oss << "lemon:FormatError" << ": "; - oss << message(); - if (!file().empty() || line() != 0) { + oss << _message; + if (!_file.empty() || _line != 0) { oss << " ("; - if (!file().empty()) oss << "in file '" << file() << "'"; - if (!file().empty() && line() != 0) oss << " "; - if (line() != 0) oss << "at line " << line(); + if (!_file.empty()) oss << "in file '" << _file << "'"; + if (!_file.empty() && _line != 0) oss << " "; + if (_line != 0) oss << "at line " << _line; oss << ")"; } _what = oss.str(); diff --git a/lemon/graph_to_eps.h b/lemon/graph_to_eps.h --- a/lemon/graph_to_eps.h +++ b/lemon/graph_to_eps.h @@ -1170,7 +1170,7 @@ std::ostream* os = new std::ofstream(file_name); if (!(*os)) { delete os; - throw IoError(file_name, "Cannot write file"); + throw IoError("Cannot write file", file_name); } return GraphToEps > (DefaultGraphToEpsTraits(g,*os,true)); @@ -1191,7 +1191,7 @@ std::ostream* os = new std::ofstream(file_name.c_str()); if (!(*os)) { delete os; - throw IoError(file_name, "Cannot write file"); + throw IoError("Cannot write file", file_name); } return GraphToEps > (DefaultGraphToEpsTraits(g,*os,true)); diff --git a/lemon/lgf_reader.h b/lemon/lgf_reader.h --- a/lemon/lgf_reader.h +++ b/lemon/lgf_reader.h @@ -516,7 +516,7 @@ _filename(fn), _digraph(digraph), _use_nodes(false), _use_arcs(false), _skip_nodes(false), _skip_arcs(false) { - if (!(*_is)) throw IoError(fn, "Cannot open file"); + if (!(*_is)) throw IoError("Cannot open file", fn); } /// \brief Constructor @@ -528,7 +528,7 @@ _filename(fn), _digraph(digraph), _use_nodes(false), _use_arcs(false), _skip_nodes(false), _skip_arcs(false) { - if (!(*_is)) throw IoError(fn, "Cannot open file"); + if (!(*_is)) throw IoError("Cannot open file", fn); } /// \brief Destructor @@ -879,7 +879,7 @@ maps.find(_node_maps[i].first); if (jt == maps.end()) { std::ostringstream msg; - msg << "Map not found in file: " << _node_maps[i].first; + msg << "Map not found: " << _node_maps[i].first; throw FormatError(msg.str()); } map_index[i] = jt->second; @@ -908,7 +908,7 @@ } } if (line >> std::ws >> c) - throw FormatError("Extra character on the end of line"); + throw FormatError("Extra character at the end of line"); Node n; if (!_use_nodes) { @@ -917,7 +917,7 @@ _node_index.insert(std::make_pair(tokens[label_index], n)); } else { if (label_index == -1) - throw FormatError("Label map not found in file"); + throw FormatError("Label map not found"); typename std::map::iterator it = _node_index.find(tokens[label_index]); if (it == _node_index.end()) { @@ -972,7 +972,7 @@ maps.find(_arc_maps[i].first); if (jt == maps.end()) { std::ostringstream msg; - msg << "Map not found in file: " << _arc_maps[i].first; + msg << "Map not found: " << _arc_maps[i].first; throw FormatError(msg.str()); } map_index[i] = jt->second; @@ -1010,7 +1010,7 @@ } } if (line >> std::ws >> c) - throw FormatError("Extra character on the end of line"); + throw FormatError("Extra character at the end of line"); Arc a; if (!_use_arcs) { @@ -1038,7 +1038,7 @@ _arc_index.insert(std::make_pair(tokens[label_index], a)); } else { if (label_index == -1) - throw FormatError("Label map not found in file"); + throw FormatError("Label map not found"); typename std::map::iterator it = _arc_index.find(tokens[label_index]); if (it == _arc_index.end()) { @@ -1073,13 +1073,13 @@ if (!_reader_bits::readToken(line, token)) throw FormatError("Attribute value not found"); if (line >> c) - throw FormatError("Extra character on the end of line"); + throw FormatError("Extra character at the end of line"); { std::set::iterator it = read_attr.find(attr); if (it != read_attr.end()) { std::ostringstream msg; - msg << "Multiple occurence of attribute " << attr; + msg << "Multiple occurence of attribute: " << attr; throw FormatError(msg.str()); } read_attr.insert(attr); @@ -1101,7 +1101,7 @@ it != _attributes.end(); ++it) { if (read_attr.find(it->first) == read_attr.end()) { std::ostringstream msg; - msg << "Attribute not found in file: " << it->first; + msg << "Attribute not found: " << it->first; throw FormatError(msg.str()); } } @@ -1117,9 +1117,6 @@ /// This function starts the batch processing void run() { LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); - if (!*_is) { - throw FormatError("Cannot find file"); - } bool nodes_done = _skip_nodes; bool arcs_done = _skip_arcs; @@ -1138,7 +1135,7 @@ _reader_bits::readToken(line, caption); if (line >> c) - throw FormatError("Extra character on the end of line"); + throw FormatError("Extra character at the end of line"); if (section == "nodes" && !nodes_done) { if (_nodes_caption.empty() || _nodes_caption == caption) { @@ -1308,7 +1305,7 @@ _filename(fn), _graph(graph), _use_nodes(false), _use_edges(false), _skip_nodes(false), _skip_edges(false) { - if (!(*_is)) throw IoError(fn, "Cannot open file"); + if (!(*_is)) throw IoError("Cannot open file", fn); } /// \brief Constructor @@ -1320,7 +1317,7 @@ _filename(fn), _graph(graph), _use_nodes(false), _use_edges(false), _skip_nodes(false), _skip_edges(false) { - if (!(*_is)) throw IoError(fn, "Cannot open file"); + if (!(*_is)) throw IoError("Cannot open file", fn); } /// \brief Destructor @@ -1715,7 +1712,7 @@ maps.find(_node_maps[i].first); if (jt == maps.end()) { std::ostringstream msg; - msg << "Map not found in file: " << _node_maps[i].first; + msg << "Map not found: " << _node_maps[i].first; throw FormatError(msg.str()); } map_index[i] = jt->second; @@ -1744,7 +1741,7 @@ } } if (line >> std::ws >> c) - throw FormatError("Extra character on the end of line"); + throw FormatError("Extra character at the end of line"); Node n; if (!_use_nodes) { @@ -1753,7 +1750,7 @@ _node_index.insert(std::make_pair(tokens[label_index], n)); } else { if (label_index == -1) - throw FormatError("Label map not found in file"); + throw FormatError("Label map not found"); typename std::map::iterator it = _node_index.find(tokens[label_index]); if (it == _node_index.end()) { @@ -1808,7 +1805,7 @@ maps.find(_edge_maps[i].first); if (jt == maps.end()) { std::ostringstream msg; - msg << "Map not found in file: " << _edge_maps[i].first; + msg << "Map not found: " << _edge_maps[i].first; throw FormatError(msg.str()); } map_index[i] = jt->second; @@ -1846,7 +1843,7 @@ } } if (line >> std::ws >> c) - throw FormatError("Extra character on the end of line"); + throw FormatError("Extra character at the end of line"); Edge e; if (!_use_edges) { @@ -1874,7 +1871,7 @@ _edge_index.insert(std::make_pair(tokens[label_index], e)); } else { if (label_index == -1) - throw FormatError("Label map not found in file"); + throw FormatError("Label map not found"); typename std::map::iterator it = _edge_index.find(tokens[label_index]); if (it == _edge_index.end()) { @@ -1909,13 +1906,13 @@ if (!_reader_bits::readToken(line, token)) throw FormatError("Attribute value not found"); if (line >> c) - throw FormatError("Extra character on the end of line"); + throw FormatError("Extra character at the end of line"); { std::set::iterator it = read_attr.find(attr); if (it != read_attr.end()) { std::ostringstream msg; - msg << "Multiple occurence of attribute " << attr; + msg << "Multiple occurence of attribute: " << attr; throw FormatError(msg.str()); } read_attr.insert(attr); @@ -1937,7 +1934,7 @@ it != _attributes.end(); ++it) { if (read_attr.find(it->first) == read_attr.end()) { std::ostringstream msg; - msg << "Attribute not found in file: " << it->first; + msg << "Attribute not found: " << it->first; throw FormatError(msg.str()); } } @@ -1972,7 +1969,7 @@ _reader_bits::readToken(line, caption); if (line >> c) - throw FormatError("Extra character on the end of line"); + throw FormatError("Extra character at the end of line"); if (section == "nodes" && !nodes_done) { if (_nodes_caption.empty() || _nodes_caption == caption) { @@ -2095,7 +2092,7 @@ SectionReader(const std::string& fn) : _is(new std::ifstream(fn.c_str())), local_is(true), _filename(fn) { - if (!(*_is)) throw IoError(fn, "Cannot open file"); + if (!(*_is)) throw IoError("Cannot open file", fn); } /// \brief Constructor @@ -2104,7 +2101,7 @@ SectionReader(const char* fn) : _is(new std::ifstream(fn)), local_is(true), _filename(fn) { - if (!(*_is)) throw IoError(fn, "Cannot open file"); + if (!(*_is)) throw IoError("Cannot open file", fn); } /// \brief Destructor @@ -2261,11 +2258,11 @@ _reader_bits::readToken(line, caption); if (line >> c) - throw FormatError("Extra character on the end of line"); + throw FormatError("Extra character at the end of line"); if (extra_sections.find(section) != extra_sections.end()) { std::ostringstream msg; - msg << "Multiple occurence of section " << section; + msg << "Multiple occurence of section: " << section; throw FormatError(msg.str()); } Sections::iterator it = _sections.find(section); @@ -2387,7 +2384,7 @@ /// file. LgfContents(const std::string& fn) : _is(new std::ifstream(fn.c_str())), local_is(true) { - if (!(*_is)) throw IoError(fn, "Cannot open file"); + if (!(*_is)) throw IoError("Cannot open file", fn); } /// \brief Constructor @@ -2396,7 +2393,7 @@ /// file. LgfContents(const char* fn) : _is(new std::ifstream(fn)), local_is(true) { - if (!(*_is)) throw IoError(fn, "Cannot open file"); + if (!(*_is)) throw IoError("Cannot open file", fn); } /// \brief Destructor diff --git a/lemon/lgf_writer.h b/lemon/lgf_writer.h --- a/lemon/lgf_writer.h +++ b/lemon/lgf_writer.h @@ -463,7 +463,7 @@ DigraphWriter(const std::string& fn, const Digraph& digraph) : _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph), _skip_nodes(false), _skip_arcs(false) { - if (!(*_os)) throw IoError(fn, "Cannot write file"); + if (!(*_os)) throw IoError("Cannot write file", fn); } /// \brief Constructor @@ -473,7 +473,7 @@ DigraphWriter(const char* fn, const Digraph& digraph) : _os(new std::ofstream(fn)), local_os(true), _digraph(digraph), _skip_nodes(false), _skip_arcs(false) { - if (!(*_os)) throw IoError(fn, "Cannot write file"); + if (!(*_os)) throw IoError("Cannot write file", fn); } /// \brief Destructor @@ -1023,7 +1023,7 @@ GraphWriter(const std::string& fn, const Graph& graph) : _os(new std::ofstream(fn.c_str())), local_os(true), _graph(graph), _skip_nodes(false), _skip_edges(false) { - if (!(*_os)) throw IoError(fn, "Cannot write file"); + if (!(*_os)) throw IoError("Cannot write file", fn); } /// \brief Constructor @@ -1033,7 +1033,7 @@ GraphWriter(const char* fn, const Graph& graph) : _os(new std::ofstream(fn)), local_os(true), _graph(graph), _skip_nodes(false), _skip_edges(false) { - if (!(*_os)) throw IoError(fn, "Cannot write file"); + if (!(*_os)) throw IoError("Cannot write file", fn); } /// \brief Destructor @@ -1585,7 +1585,7 @@ /// Construct a section writer, which writes into the given file. SectionWriter(const std::string& fn) : _os(new std::ofstream(fn.c_str())), local_os(true) { - if (!(*_os)) throw IoError(fn, "Cannot write file"); + if (!(*_os)) throw IoError("Cannot write file", fn); } /// \brief Constructor @@ -1593,7 +1593,7 @@ /// Construct a section writer, which writes into the given file. SectionWriter(const char* fn) : _os(new std::ofstream(fn)), local_os(true) { - if (!(*_os)) throw IoError(fn, "Cannot write file"); + if (!(*_os)) throw IoError("Cannot write file", fn); } /// \brief Destructor