Take care to close the directory if we get an exception from std::stringstream