Search code examples
c++exceptionistream

C++ Throwing istream specific errors when parsing a file


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.


Solution

  • When an input operation fails, a "failbit" is set on the stream.

    You can check for this with an "if" statement:

    input >> neighbours[i];
    if (!input) {
       std::cout << "Error: Int Neighbour" << std::endl;
    }
    

    Or:

    if (!(input >> neighbours[i])) {
       std::cout << "Error: Int Neighbour" << std::endl;
    }
    

    However, as well as couting, you're also going to need to do something with that bad input. If you're not planning to just "return", you will have to skip a line, or skip some number of bytes, or do whatever it is that you think is appropriate. Also clear the error state with std::cout.clear(), otherwise no further input operations will succeed.