I have the following code:
for (const auto& x : std::filesystem::directory_iterator(dir)) {
// do stuff with x
}
dir might not exist, and I want to treat this case as if the dir is empty. I can't seem to come up with a nice option.
- If I guard everything with
try/catch, then I'll be catching the iteration code exceptions as well, I don't want that. - If I move
std::filesystem::directory_iteratorconstruction up and guard it withtry/catch, it becomes verbose, and I'll have to re-throw all other exceptions (won't it screw up stack traces and such?). - If I use the non-throwing constructor of
directory_iterator, I'll have to throwstd::error_codefor other errors. I'm not sure how to do that.