I want to do read from a file using C++ standard library facilities (std::ifstream
) - while, of course, reliably reporting errors if I encounter them.
Apparently, this is not at all an easy task!
std::basic_fstream
's (the template of whichstd::ifstream
is an instantiation) do not throw exceptions by default.- You can make a basic fstream throw exceptions - but only after construction, so the construction won't fail. See
basic_ios::exceptions()
(that's a super-superclass ofstd::ifstream
).
14 years ago, this question was asked:
Get std::fstream failure error messages and/or exceptions
and the answers tell us that:
- There are no guarantees the thrown exception will tell us what the cause of the error was (just that some error occurred)
- We are not guaranteed that when a failbit or badbit is set on an fstream,
errno
/GetLastError()
provide us with a non-zero/non-success value.
That's quite bad. On the other hand, 14 years have passed. Has anything changed? That is, are there better guarantees regarding the thrown exception or of errno
/ GetLastError()
being set? If not, what is the "best effort" approach to reporting an error in the construction of an std::fstream
?
(I am tempted to ask "why the **** does the constructor not throw on failure, but let's not talk about that.)