std::filesystem::filesystem_error::what
来自 cppreference.com
< cpp | filesystem | filesystem error
const char* what() const noexcept 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_error const& 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_error const& ex) { explain("fs::rename()", ex); } for (auto const path : {"/bool", "/bin/cat", "/bin/mouse"}) try { std::filesystem::create_directory(path); } catch(fs::filesystem_error const& 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(): ""