Spec-Zone.ru › C++

std::filesystem::filesystem_error::what

const char* what() const noexcept override;
(начиная с C++17)

Возвращает поясняющую байтовую строку. Эта поясняющая строка содержит поясняющую строку, переданную при создании объекта. Реализациям рекомендуется также включать в возвращаемую строку пути к path1() и path2() в собственном формате, а также строку std::system_error::what().

Параметры

нет

Возвращаемое значение

Поясняющая байтовая строка в стиле C, содержащая поясняющую строку, переданную при создании объекта.

Пример

#include <cstdio>
#include <filesystem>
#include <iostream>
#include <string_view>
namespace fs = std::filesystem;

void explain(std::string_view note, fs::filesystem_error const& ex)
{
    std::cout << note << " exception:\n"
              << "what(): " << ex.what() << '\n'
              << "path1(): " << ex.path1() << ", path2(): "
              << ex.path2() << "\n\n";
}

int main()
{
    try
    {
        std::filesystem::rename("/dev", "/null");
    }
    catch(fs::filesystem_error const& ex)
    {
        explain("fs::rename()", ex);
    }

    for (auto const path : {"/bool", "/bin/cat", "/bin/mouse"})
        try
        {
            std::filesystem::create_directory(path);
        }
        catch(fs::filesystem_error const& ex)
        {
            explain("fs::create_directory()", ex);
        }
}

Возможный вывод:

fs::rename() exception:
what(): filesystem error: cannot rename: Permission denied [/dev] [/null]
path1(): "/dev", path2(): "/null"

fs::create_directory() exception:
what(): filesystem error: cannot create directory: Permission denied [/bool]
path1(): "/bool", path2(): ""

fs::create_directory() exception:
what(): filesystem error: cannot create directory: File exists [/bin/cat]
path1(): "/bin/cat", path2(): ""

fs::create_directory() exception:
what(): filesystem error: cannot create directory: Read-only file system [/bin/mouse]
path1(): "/bin/mouse", path2(): ""

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/filesystem/filesystem_error/what

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API