src: added exception handling to file reading in TextAccess

This commit is contained in:
Eberhard Graether
2019-01-08 14:10:08 +01:00
parent b4ea675c9a
commit 5b787e19d4
+26 -12
View File
@@ -138,22 +138,36 @@ std::vector<std::string> TextAccess::readFile(const FilePath& filePath)
{
std::vector<std::string> result;
std::ifstream srcFile;
srcFile.open(filePath.str());
if (srcFile.fail())
try
{
LOG_ERROR(L"Could not open file " + filePath.wstr());
return result;
}
std::ifstream srcFile;
srcFile.open(filePath.str());
while (!srcFile.eof())
if (srcFile.fail())
{
LOG_ERROR(L"Could not open file " + filePath.wstr());
return result;
}
while (!srcFile.eof())
{
std::string line;
safeGetline(srcFile, line);
result.push_back(line + '\n');
}
srcFile.close();
}
catch (std::exception& e)
{
std::string line;
safeGetline(srcFile, line);
result.push_back(line + '\n');
LOG_ERROR_STREAM(<< "Exception thrown while reading file \"" << filePath.str() << "\": " << e.what());
result.clear();
}
catch (...)
{
LOG_ERROR_STREAM(<< "Unknown exception thrown while reading file \"" << filePath.str() << "\"");
result.clear();
}
srcFile.close();
if (!result.empty())
{