Hi I have this line in my main method:
std::copy(std::istream_iterator<Constituency>(inputFile), std::istream_iterator<Constituency>(), std::back_inserter(constits));
This parses a file into a vector. I have overwritten the std::istream <<
operator overload and am looking for ways to throw specific error messages if parsing fails. Here is the << operator overload:
std::istream& operator>> (std::istream& input, Constituency& constituency) {
int num_neighbours;
input >> num_neighbours;
std::string name;
std::vector<int> neighbours(num_neighbours);
for(int i = 0; i < num_neighbours; i++) {
try{
input >> neighbours[i];
} catch(...) {
std::cout << "Error: Int Neighbour" << std::endl;
}
}
try{
input >> name;
} catch(...) {
std::cout << "Error: Expected String Name" << std::endl;
}
constituency = Constituency(name, neighbours);
return input;
}
The error messages don't get printed out. How can I change this so that if a string is encountered where an int is expected it throws and error and vice versa.