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(): ""