Can a file stream destructor throw an exception in C++?

Viewed 953

Can a file stream destructor throw an exception, e.g., if file closing operation fails?

   auto f = new std::ofstream("data.txt");
   f->exceptions(std::ofstream::failbit | std::ofstream::badbit);
   ...
   delete f; // May throw?     

Can I prevent such exceptions by closing a stream manually?

   auto f = new std::ofstream("data.txt");
   f->exceptions(std::ofstream::failbit | std::ofstream::badbit);
   ...
   f->close();
   delete f; // May throw?     
1 Answers
Related