std::filesystem::filesystem_error:: what
From cppreference.net
<
cpp
|
filesystem
|
filesystem error
C++
filesystem_error
| Member functions | ||||
|
filesystem_error::what
|
||||
| Inherited from std::system_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(): ""