Namespaces
Variants

std::filesystem::filesystem_error:: what

From cppreference.net
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 << " 例外:\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() 例外:
what(): filesystem error: cannot rename: Permission denied [/dev] [/null]
path1(): "/dev", path2(): "/null"
fs::create_directory() 例外:
what(): filesystem error: cannot create directory: Permission denied [/bool]
path1(): "/bool", path2(): ""
fs::create_directory() 例外:
what(): filesystem error: cannot create directory: File exists [/bin/cat]
path1(): "/bin/cat", path2(): ""
fs::create_directory() 例外:
what(): filesystem error: cannot create directory: Read-only file system [/bin/mouse]
path1(): "/bin/mouse", path2(): ""