Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::filesystem::filesystem_error

      From cppreference.com
      <cpp‎ |filesystem
       
       
      Filesystem library
      Classes
      Functions
      File types
       
       
      Defined in header<filesystem>
      class filesystem_error;
      (since C++17)

      The classstd::filesystem::filesystem_error defines an exception object that is thrown on failure by the throwing overloads of the functions in the filesystem library.

      std-filesystem-filesystem error-inheritance.svg

      Contents

      [edit]Member functions

      constructs the exception object
      (public member function)[edit]
      replaces the exception object
      (public member function)[edit]
      returns the paths that were involved in the operation that caused the error
      (public member function)[edit]
      returns the explanatory string
      (public member function)[edit]

      Inherited fromstd::system_error

      Member functions

      returns error code
      (public member function ofstd::system_error)[edit]
      [virtual]
      returns an explanatory string
      (virtual public member function ofstd::system_error)[edit]

      Inherited fromstd::runtime_error


      Inherited fromstd::exception

      Member functions

      [virtual]
      destroys the exception object
      (virtual public member function ofstd::exception)[edit]
      [virtual]
      returns an explanatory string
      (virtual public member function ofstd::exception)[edit]

      [edit]Notes

      In order to ensure that copy functions offilesystem_error are noexcept, typical implementations store an object holding the return value ofwhat() and twostd::filesystem::path objects referenced bypath1() andpath2() respectively in a separately-allocated reference-counted storage.

      Currently theMS STL implementation is non-conforming: objects mentioned above are stored directly in thefilesystem object, which makes the copy functions not noexcept.

      [edit]Example

      Run this code
      #include <filesystem>#include <iostream>#include <system_error> int main(){conststd::filesystem::path from{"/none1/a"}, to{"/none2/b"}; try{std::filesystem::copy_file(from, to);// throws: files do not exist}catch(std::filesystem::filesystem_errorconst& 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 equivalentsstd::error_code ec;std::filesystem::copy_file(from, to, ec);// does not throwstd::cout<<"\nNon-throwing form sets error_code: "<< ec.message()<<'\n';}

      Possible output:

      what():  filesystem error: cannot copy file: No such file or directory [/none1/a] [/none2/b]path1(): "/none1/a"path2(): "/none2/b"code().value():    2code().message():  No such file or directorycode().category(): generic Non-throwing form sets error_code: No such file or directory
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/filesystem/filesystem_error&oldid=158002"

      [8]ページ先頭

      ©2009-2025 Movatter.jp