2 ms·
It's goofy, but one of the reasons I'll pick C for a quick program is that both Windows and Linux will give better error messages for standard functions that fa
by maxlybbert 10y ago
It's goofy, but one of the reasons I'll pick C for a quick program is that both Windows and Linux will give better error messages for standard functions that fail and set errno than for equivalent C++ code that throws an exception.
This isn't a question about the language, it's definitely a quality-of-implementation issue for standard exceptions to have what() give a useful message.
Just to clarify perror will generally give a better error message in:
FILE* fh = fopen("foo.txt", "w+");
if (!fh) {
perror("unable to open foo.txt");
return 1;
}
Than what() will in:
std::fstream fs;
fs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
fs.open("too.txt");
}
catch (const std::exception& ex)
{
std::cerr << "unable to open foo.txt: "
<< ex.what() << '\n';
}
(Ignoring other issues such as whether exceptions are better than return codes, etc.)
- mtdewcmu 10y agoA lot of people seem to use stdio even in C++. Having comprehensible error messages is related to being a simple language, IMO. C++ compile-time errors are notorious for being unreadable. I'm not familiar with the output of what(), but I'd wager that the problem is a consequence of all the abstraction.