Namespaces
Variants

std::filesystem:: filesystem_error

From cppreference.net
定義済みヘッダー <filesystem>
class filesystem_error ;
(C++17以降)

std::filesystem::filesystem_error クラスは、ファイルシステムライブラリ内の関数の例外送出オーバーロードが失敗した際に送出される例外オブジェクトを定義します。

cpp/error/exception cpp/error/runtime error cpp/error/system error std-filesystem-filesystem error-inheritance.svg

継承図

目次

メンバー関数

例外オブジェクトを構築する
(public member function)
例外オブジェクトを置き換える
(public member function)
エラーの原因となった操作に関連するパスを返す
(public member function)
説明文字列を返す
(public member function)

std::system_error から継承

メンバ関数

エラーコードを返す
( std::system_error の公開メンバ関数)
[virtual]
説明文字列を返す
( std::system_error の仮想公開メンバ関数)

std::exception から継承 std:: exception

メンバ関数

例外オブジェクトを破棄
( std::exception の仮想公開メンバ関数)
[virtual]
説明文字列を返す
( std::exception の仮想公開メンバ関数)

注記

filesystem_error のコピー関数がnoexceptであることを保証するため、一般的な実装では、 what() の戻り値を保持するオブジェクトと、 path1() および path2() によってそれぞれ参照される2つの std::filesystem::path オブジェクトを、別途確保された参照カウント方式のストレージに格納します。

現在、 MS STL実装 は規格に準拠していません:前述のオブジェクトは filesystem オブジェクト内に直接格納されているため、コピー関数がnoexceptになりません。

#include <filesystem>
#include <iostream>
#include <system_error>
int main()
{
    const std::filesystem::path from{"/none1/a"}, to{"/none2/b"};
    try
    {
        std::filesystem::copy_file(from, to); // スロー: ファイルが存在しない
    }
    catch (std::filesystem::filesystem_error const& ex)
    {
        std::cout << "what():  " << ex.what() << '\n'
                  << "path1(): " << ex.path1() << '\n'
                  << "path2(): " << ex.path2() << '\n'
                  << "code().value():    " << ex.code().value() << '\n'
                  << "code().message():  " << ex.code().message() << '\n'
                  << "code().category(): " << ex.code().category().name() << '\n';
    }
    // すべての関数には非スロー版が存在する
    std::error_code ec;
    std::filesystem::copy_file(from, to, ec); // スローしない
    std::cout << "\n非スロー形式はerror_codeを設定: " << ec.message() << '\n';
}

出力例:

what():  filesystem error: cannot copy file: No such file or directory [/none1/a] [/none2/b]
path1(): "/none1/a"
path2(): "/none2/b"
code().value():    2
code().message():  No such file or directory
code().category(): generic
Non-throwing form sets error_code: No such file or directory