From 968325e6a734e3967c1aff15138f72b6b13edf0b Mon Sep 17 00:00:00 2001 From: rulrich <ralf.m.ulrich@kit.edu> Date: Fri, 24 Jul 2020 13:38:07 +0200 Subject: [PATCH] better error messages --- readLib/source/Interface.boost.cc | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/readLib/source/Interface.boost.cc b/readLib/source/Interface.boost.cc index 7a54baf..31ef327 100644 --- a/readLib/source/Interface.boost.cc +++ b/readLib/source/Interface.boost.cc @@ -21,6 +21,7 @@ namespace corsika_data { struct DataFile { std::ifstream file_in_; + std::string file_name_; boost::iostreams::filtering_istream in_; DataFile() {} ~DataFile() {} @@ -34,10 +35,12 @@ namespace corsika_data { auto i1 = s.find_first_not_of(" \t\r\n"); // trimm right auto i2 = s.find_first_of(" \t\r\n", i1); // trimm left std::string trimmed(s.substr(i1, i2 - i1)); + file_name_ = trimmed; file_in_.open(trimmed, ios_base::in | ios_base::binary); if (!file_in_ || !file_in_.good()) { - std::cerr << std::string("Cannot open file: \'") + trimmed << "\'" << std::endl; - throw std::runtime_error(std::string("Cannot open file: ") + trimmed); + std::cerr << std::string("Cannot open file: \'") + file_name_ << "\'" + << std::endl; + throw std::runtime_error(std::string("Cannot open file: ") + file_name_); } if (trimmed.rfind(".bz2") == trimmed.length() - 4) in_.push(boost::iostreams::bzip2_decompressor()); @@ -48,20 +51,27 @@ namespace corsika_data { for (int i = 0; i < length; i++) { in_ >> data[i]; if (!in_.good()) { - throw std::runtime_error(std::string("Cannot FillArray i=") + - std::to_string(i)); + throw std::runtime_error(std::string("Cannot FillArray i/length=") + + std::to_string(i) + "/" + std::to_string(length) + + " in file " + file_name_); } } } double ReadNextNumber() { double data; in_ >> data; - if (!in_.good()) { throw std::runtime_error(std::string("Cannot ReadNextNumber")); } + if (!in_.good()) { + throw std::runtime_error( + std::string("Cannot ReadNextNumber in file " + file_name_)); + } return data; } int ReadNextText(std::string& data) { std::getline(in_, data, ' '); - if (!in_.good()) { throw std::runtime_error(std::string("Cannot ReadNextText")); } + if (!in_.good()) { + throw std::runtime_error( + std::string("Cannot ReadNextText in file " + file_name_)); + } return 0; } }; -- GitLab