最終更新日時(UTC):
が更新

履歴 編集

function
<filesystem>

std::filesystem::filesystem_error::コンストラクタ(C++17)

filesystem_error(const string& what_arg,
                 error_code ec);         // (1)

filesystem_error(const string& what_arg,
                 const path& p1,
                 error_code ec);         // (2)

filesystem_error(const string& what_arg,
                 const path& p1,
                 const path& p2,
                 error_code ec);         // (3)

概要

  • (1) : エラー理由の文字列と、エラーコードを受け取るコンストラクタ
  • (2) : エラー理由の文字列、エラーとなったパス、エラーコードを受け取るコンストラクタ
  • (3) : エラー理由の文字列、エラーとなったパスを一組と、エラーコードを受け取るコンストラクタ

事後条件

  • (1) :
    • code()メンバ関数の戻り値 : ec
    • path1()メンバ関数の戻り値 : 空のパス
    • path2()メンバ関数の戻り値 : 空のパス
    • エラー理由の文字列:
  • (2) :
    • code()メンバ関数の戻り値 : ec
    • path1()メンバ関数の戻り値 : コピーされたp1への参照
    • path2()メンバ関数の戻り値 : 空のパス
    • エラー理由の文字列:
  • (3) :
    • code()メンバ関数の戻り値 : ec
    • path1()メンバ関数の戻り値 : コピーされたp1への参照
    • path2()メンバ関数の戻り値 : コピーされたp2への参照
    • エラー理由の文字列:

#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;

int main()
{
  // (1)
  try {
    throw fs::filesystem_error(
      "can't copy file. source file doesn't found",
      std::make_error_code(std::errc::no_such_file_or_directory)
    );
  }
  catch (fs::filesystem_error& err) {
    std::cout << err.what() << std::endl;
  }

  // (2)
  try {
    throw fs::filesystem_error(
      "can't copy file. source file doesn't found",
      "a/b.txt",
      std::make_error_code(std::errc::no_such_file_or_directory)
    );
  }
  catch (fs::filesystem_error& err) {
    std::cout << err.what() << std::endl;
  }

  // (3)
  try {
    throw fs::filesystem_error(
      "can't copy file. source file doesn't found",
      "a/from.txt",
      "b/to.txt",
      std::make_error_code(std::errc::no_such_file_or_directory)
    );
  }
  catch (fs::filesystem_error& err) {
    std::cout << err.what() << std::endl;
  }
}

出力例

filesystem error: can't copy file. source file doesn't found: No such file or directory
filesystem error: can't copy file. source file doesn't found: No such file or directory [a/b.txt]
filesystem error: can't copy file. source file doesn't found: No such file or directory [a/from.txt] [b/to.txt]

バージョン

言語

  • C++17

処理系

参照