std::filesystem::filesystem_error::what
来自cppreference.com
< cpp | filesystem | filesystem error
constchar* what()constnoexcept 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_errorconst& ex){std::cout<< note <<" exception:\n"<<"what(): "<< ex.what()<<'\n'<<"path1(): "<< ex.path1()<<", path2(): "<< ex.path2()<<"\n\n";} int main(){try{std::filesystem::rename("/dev", "/null");}catch(fs::filesystem_errorconst& ex){ explain("fs::rename()", ex);} for(autoconst path :{"/bool", "/bin/cat", "/bin/mouse"})try{std::filesystem::create_directory(path);}catch(fs::filesystem_errorconst& ex){ explain("fs::create_directory()", ex);}}
可能的输出:
fs::rename() exception: what(): filesystem error: cannot rename: Permission denied [/dev] [/null] path1(): "/dev", path2(): "/null" fs::create_directory() exception: what(): filesystem error: cannot create directory: Permission denied [/bool] path1(): "/bool", path2(): "" fs::create_directory() exception: what(): filesystem error: cannot create directory: File exists [/bin/cat] path1(): "/bin/cat", path2(): "" fs::create_directory() exception: what(): filesystem error: cannot create directory: Read-only file system [/bin/mouse] path1(): "/bin/mouse", path2(): ""