std::filesystem::filesystem_error::what
来自 cppreference.cn
< cpp | filesystem | filesystem error
const char* what() const noexcept override; |
(since 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(): ""