-
C++ wasn’t built with exceptions from day one. “C with classes” started in 1979, and exceptions were added in 1989. Meanwhile, the
streamslibrary was written as early as 1984 (later becomesiostreamsin 1989 (later reimplemented by GNU in 1991)), it just cannot use exception handling in the beginning.Ref:
- Bjarne Stroustrup, A History of C++: 1979−1991
- C++ Libraries
-
You can enable exceptions with the
.exceptionsmethod.
// ios::exceptions
#include <iostream>
#include <fstream>
#include <string>
int main () {
std::ifstream file;
file.exceptions(ifstream::failbit | ifstream::badbit);
try {
file.open ("test.txt");
std::string buf;
while (std::getline(file, buf))
std::cout << "Read> " << buf << "\n";
}
catch (ifstream::failure& e) {
std::cout << "Exception opening/reading file\n";
}
}