From 5b787e19d4862035040f5ab9251fb4d23de23e3e Mon Sep 17 00:00:00 2001 From: Eberhard Graether Date: Tue, 8 Jan 2019 14:10:08 +0100 Subject: [PATCH] src: added exception handling to file reading in TextAccess --- src/lib/utility/text/TextAccess.cpp | 38 ++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/src/lib/utility/text/TextAccess.cpp b/src/lib/utility/text/TextAccess.cpp index 535e12ae..a6cab716 100644 --- a/src/lib/utility/text/TextAccess.cpp +++ b/src/lib/utility/text/TextAccess.cpp @@ -138,22 +138,36 @@ std::vector TextAccess::readFile(const FilePath& filePath) { std::vector 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()) {