V
V
Vladimir A2020-10-24 10:08:10
C++ / C#
Vladimir A, 2020-10-24 10:08:10

How to return an error in std::filesystem?

For example, this is how I do it using runtime_error:

#include <exception>
#include <filesystem>

std::exception &foo(){
    try {
        if(!std::filesystem::is_directory(std::filesystem::path("sdfsdf"))){
            throw std::runtime_error("Error14");
        }
    }
    catch (std::exception &e) {
        return e;
    }
}

int main(){
    foo().what();
    // далее обработка принятого сообщения
}


how to do something similar, but with error return from std::filesystem, I mean

std::filesystem::filesystem_error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Armenian Radio, 2020-10-24
@hauptling

For example, there is such an addendum at https://en.cppreference.com/w/cpp/filesystem/files...

In order to ensure that copy functions of filesystem_error are noexcept, typical implementations store an object holding the return value of what() and two std::filesystem::path objects referenced by path1() and path2() respectively in a separately-allocated reference-counted storage.
Currently the MS STL implementation is non-conforming: the objects mentioned above are stored directly in the filesystem object, which makes the copy functions not noexcept.

from which it follows that the object of the exception should not be copied so freely

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question