名前空間
変種
操作

std::filesystem::filesystem_error

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

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

cpp/error/exceptioncpp/error/runtime errorcpp/error/system errorstd-filesystem-filesystem error-inheritance.svg

継承図

目次

[編集] メンバ関数

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

std::system_error から継承

メンバ関数

エラーコードを返します。
(std::system_error の public member function) [編集]
[virtual]
説明文字列を返す
(std::system_error の virtual public member function) [編集]

std::runtime_error から継承


std::exception から継承

メンバ関数

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

[編集] 注意

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); // throws: files do not exist
    }
    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';
    }
 
    // All functions have non-throwing equivalents
    std::error_code ec;
    std::filesystem::copy_file(from, to, ec); // does not throw
    std::cout << "\nNon-throwing form sets 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
English 日本語 中文(简体) 中文(繁體)