If the file does not exist LemonReader throws exception
authordeba
Fri, 28 Oct 2005 08:40:42 +0000
changeset 1746874e4bc21435
parent 1745 d356e54bdafc
child 1747 bccf2379b5dd
If the file does not exist LemonReader throws exception
lemon/error.h
lemon/lemon_reader.h
     1.1 --- a/lemon/error.h	Wed Oct 26 11:10:18 2005 +0000
     1.2 +++ b/lemon/error.h	Fri Oct 28 08:40:42 2005 +0000
     1.3 @@ -326,6 +326,56 @@
     1.4      virtual ~DataFormatError() throw() {}
     1.5    };
     1.6  
     1.7 +  ///\e 
     1.8 +  class FileOpenError : public IOError {
     1.9 +  protected:
    1.10 +    ExceptionMember<std::string> _file;
    1.11 +
    1.12 +    mutable ExceptionMember<std::string> _message_holder;
    1.13 +  public:
    1.14 +
    1.15 +    FileOpenError(const FileOpenError &foe) : 
    1.16 +      IOError(foe), _file(foe._file) {}
    1.17 +
    1.18 +    ///\e 
    1.19 +    explicit FileOpenError(const std::string& file)
    1.20 +      : _file(file) {}
    1.21 +
    1.22 +
    1.23 +    ///\e 
    1.24 +    void file(const std::string &file) { _file.set(file); }
    1.25 + 
    1.26 +    /// \brief Returns the filename.
    1.27 +    ///
    1.28 +    /// Returns \e null if the filename was not specified.
    1.29 +    const char* file() const {
    1.30 +      if (_file.valid() && !_file.get().empty()) {
    1.31 +	return _file.get().c_str();
    1.32 +      } else {
    1.33 +	return 0;
    1.34 +      }
    1.35 +    }
    1.36 +
    1.37 +    ///\e 
    1.38 +    virtual const char* what() const throw() {
    1.39 +      try {
    1.40 +	std::ostringstream ostr;
    1.41 +	ostr << exceptionName() << ": ";
    1.42 +	ostr << "Cannot open file - " << file();
    1.43 +	_message_holder.set(ostr.str());
    1.44 +      }
    1.45 +      catch (...) {}
    1.46 +      if( _message_holder.valid()) return _message_holder.get().c_str();
    1.47 +      return exceptionName();
    1.48 +    }
    1.49 +
    1.50 +    virtual const char* exceptionName() const {
    1.51 +      return "lemon::FileOpenError";
    1.52 +    }
    1.53 +
    1.54 +    virtual ~FileOpenError() throw() {}
    1.55 +  };
    1.56 +
    1.57    class IOParameterError : public LogicError {
    1.58    protected:
    1.59      ExceptionMember<std::string> _message;
     2.1 --- a/lemon/lemon_reader.h	Wed Oct 26 11:10:18 2005 +0000
     2.2 +++ b/lemon/lemon_reader.h	Fri Oct 28 08:40:42 2005 +0000
     2.3 @@ -409,6 +409,9 @@
     2.4      LemonReader(const std::string& filename) 
     2.5        : is(0), own_is(true) {
     2.6        is = new std::ifstream(filename.c_str());
     2.7 +      if (is->fail()) {
     2.8 +	throw FileOpenError(filename);
     2.9 +      }
    2.10      }
    2.11  
    2.12      /// \brief Desctructor for LemonReader.