# HG changeset patch # User deba # Date 1130488842 0 # Node ID 874e4bc21435b235b33af29a0c4a16b8c0443992 # Parent d356e54bdafc10b862d8eac8ac50e5345e742a4e If the file does not exist LemonReader throws exception diff -r d356e54bdafc -r 874e4bc21435 lemon/error.h --- a/lemon/error.h Wed Oct 26 11:10:18 2005 +0000 +++ b/lemon/error.h Fri Oct 28 08:40:42 2005 +0000 @@ -326,6 +326,56 @@ virtual ~DataFormatError() throw() {} }; + ///\e + class FileOpenError : public IOError { + protected: + ExceptionMember _file; + + mutable ExceptionMember _message_holder; + public: + + FileOpenError(const FileOpenError &foe) : + IOError(foe), _file(foe._file) {} + + ///\e + explicit FileOpenError(const std::string& file) + : _file(file) {} + + + ///\e + void file(const std::string &file) { _file.set(file); } + + /// \brief Returns the filename. + /// + /// Returns \e null if the filename was not specified. + const char* file() const { + if (_file.valid() && !_file.get().empty()) { + return _file.get().c_str(); + } else { + return 0; + } + } + + ///\e + virtual const char* what() const throw() { + try { + std::ostringstream ostr; + ostr << exceptionName() << ": "; + ostr << "Cannot open file - " << file(); + _message_holder.set(ostr.str()); + } + catch (...) {} + if( _message_holder.valid()) return _message_holder.get().c_str(); + return exceptionName(); + } + + virtual const char* exceptionName() const { + return "lemon::FileOpenError"; + } + + virtual ~FileOpenError() throw() {} + }; + class IOParameterError : public LogicError { protected: ExceptionMember _message; diff -r d356e54bdafc -r 874e4bc21435 lemon/lemon_reader.h --- a/lemon/lemon_reader.h Wed Oct 26 11:10:18 2005 +0000 +++ b/lemon/lemon_reader.h Fri Oct 28 08:40:42 2005 +0000 @@ -409,6 +409,9 @@ LemonReader(const std::string& filename) : is(0), own_is(true) { is = new std::ifstream(filename.c_str()); + if (is->fail()) { + throw FileOpenError(filename); + } } /// \brief Desctructor for LemonReader.