Do I need to manually close an ifstream?

Do I need to manually call close() when I use a std::ifstream? For example, in the code: std::string readContentsOfFile(std::string fileName) { std::ifstream file(fileName.c_str()); if (file.good()) { std::stringstream buffer; buffer << file.rdbuf(); file.close(); return buffer.str(); } throw std::runtime_exception(“file not found”); } Do I need to call file.close() manually? Shouldn’t ifstream make use of RAII for closing … Read more

If you shouldn’t throw exceptions in a destructor, how do you handle errors in it?

Most people say never throw an exception out of a destructor – doing so results in undefined behavior. Stroustrup makes the point that “the vector destructor explicitly invokes the destructor for every element. This implies that if an element destructor throws, the vector destruction fails… There is really no good way to protect against exceptions … Read more