#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <limits>

void do_without_exceptions()
{
    std::istringstream in("3 4\n5 6\nbad line\n10 39\n100 1001\nbad line");

    while (in.good())
    {
        int a, b;
        while (in >> a >> b)
            std::cout << a << " + " << b << " = " << a + b << '\n';

        if (!in)
        {
            in.clear();
            in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
    }
}

void do_with_exceptions()
{
	auto exception_bits = std::istringstream::badbit |
	                      std::istringstream::failbit |
	                      std::istringstream::eofbit;
	
    std::istringstream in("3 4\n5 6\nbad line\n10 39\n100 1001\nbad line");
    in.exceptions(exception_bits);

    while (in.good())
    {
        try {
            int a, b;
            in >> a >> b;
            std::cout << a << " + " << b << " = " << a + b << '\n';
        }

        catch (std::istream::failure& ex)
        {
            if (in.fail())
            {
                try {
                    in.clear();
                    in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                }

                catch (std::istream::failure&)
                {
                }
            }
        }
    }
}


int main(int argc, char* argv[])
{
    do_without_exceptions();
    do_with_exceptions();
}