std::filesy..._error::what(3)
β¨ std::filesystem::filesystem_error::what β¨
okay bestie, so this is like, the manual page for std::filesystem::filesystem_error::what, which lives in the C++ Standard Libary (yes that's a typo in the original, we're keeping it, it's iconic). it's giving "exception tea" and honestly? we're obsessed. let me spill π
πΈ Synopsis πΈ
const char* what() const noexcept override; (since C++17)
so like, since C++17, this lil function is const, it's noexcept, and it overrides. periodt.
π Description π
okay so what() returns an explanatory byte string. and like, this explanatory string contains the explanatory string that was passed at the time of construction. i know, very "the what contains the what," very main character energy. π
implementations are encouraged to include the pathnames of path1() and path2() in native format, and also the std::system_error::what() string inside the returned string as well. so like, the vibes are "here's what went down, here's the paths, and here's the system error lore." no cap. β¨
π¦ Parameters π¦
(none)β literally nothing, empty, the void, a blank canvas, she takes no arguments, she's low maintenance like that π
π Return value π
a C-style explanatory byte string (the original says "C-stye" and we're keeping that too, it's a moment) that contains the explanatory string passed at the time of construction. so like, what you put in is what you get out. fr fr. πΈ
π₯ Example π₯
run this code bestie, it's a whole vibe
#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);
}
}
π Possible output π
like, this is what it might look like when you run it, depending on your system and your whole situation:
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(): ""
like, the what() string is doing the absolute most, slaying the explanation, and then path1() and path2() are there too, giving coordinates. iconic. π«Ά
ngl bestie, this function is literally just "tell me what went wrong" but make it C++ and make it noexcept. very demure, very mindful.
okay that's the whole page! this was from http://cppreference.com, dated 2024.06.10, and the manual is std::filesy..._error::what(3) β yes the title got truncated, it's giving "the way that the manpage tool can't fit filesystem_error in the header," we still love her. bye bestie, go catch those filesystem errors πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms