Are these try/catch blocks cluttering the code? The following program reads a stream and creates a two dimensional std::vector.

I'm trying to find an elegant way to handle the errors and recover from them as much as possible while still reporting the problems encountered. 

It feels as though my nice clean functions are getting more and more cluttered as I add the error handling. 

Is this the best way?

#include &lt;vector&gt;
#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;
#include &lt;iomanip&gt;
#include &lt;stdexcept&gt;

namespace {

    using Grid_row = std::vector&lt;int&gt;;
    using Grid = std::vector&lt;Grid_row&gt;;

    class Partial_value : public std::runtime_error {
    public:
        Partial_value(std::string text, int val) : std::runtime_error{text}, val{val} {}
        int val{};
    };

    int convert(const std::string&amp; text)
    {
        int val{};
        try {
            val = std::stoi(text);
        }
        catch(const std::exception&amp; e) {
            throw Partial_value{"Due to error: \"" + std::string{e.what()} +"\"; Could not convert " + text + " to integer. Value was converted to 0.", val};
        }
        std::ostringstream oss{};
        oss &lt;&lt; val;
        if(oss.str() != text)
            throw Partial_value{"Could not fully convert " + text + " to integer. Value was converted to " + oss.str() + ".", val};
        return val;
    }

    std::istream&amp; operator &gt;&gt; (std::istream&amp; is, Grid_row&amp; grid_row)
    {
        std::string term{};
        while(is &gt;&gt; term) {
            int val{};
            try {
                val = convert(term);
            }
            catch(const Partial_value&amp; e) {
                val = e.val;
                std::cerr &lt;&lt; e.what() &lt;&lt; '\n';
            }
            grid_row.push_back(val);
        }
        return is;
    }

    std::istream&amp; operator &gt;&gt; (std::istream&amp; is, Grid&amp; grid)
    {
        std::string line{};
        while(std::getline(is, line)) {
            std::istringstream is_line{line};
            Grid_row grid_row{};
            is_line &gt;&gt; grid_row;
            grid.push_back(grid_row);
        }
        return is;
    }

    std::ostream&amp; operator &lt;&lt; (std::ostream&amp; os, const Grid&amp; grid)
    {
        for(const auto&amp; i : grid) {
            for(const auto&amp; j : i) {
                os &lt;&lt; std::setw(5) &lt;&lt; j;
            }
            os &lt;&lt; '\n';
        }
        return os;
    }
}

auto input_text{
R"(3 -1 -3 -1 -4
40 7.5 3 1 1
10 -2 -Z 1 1
10 0 1 0 -1
)"};

int main() {
    std::istringstream is{input_text};
    Grid grid{};
    is &gt;&gt; grid;
    std::cout &lt;&lt; grid;
    return 0;
}


Produces:

Could not fully convert 7.5 to integer. Value was converted to 7.
Due to error: "invalid stoi argument"; Could not convert -Z to integer. Value was converted to 0.
    3   -1   -3   -1   -4
   40    7    3    1    1
   10   -2    0    1    1
   10    0    1    0   -1
Your convert function should be pure.

convert should be doing one thing and one thing only, and that is to convert the given string to int. All those checks you do are not useful because they can easily be performed in the function that calls convert.

int convert(const std::string&amp; text) {
    return std::stoi(text);
}


Your convert function is not needed.

Seeing as after changing it to a pure function, it is now acting as a wrapper to another function call, you don't need it anymore, and it can now be replaced by the function it calls, std::stoi.

Move convert logic elsewhere

Move all the logic from within the convert method to your overloaded function, std::istream&amp; operator &gt;&gt; (std::istream&amp; is, Grid_row&amp; grid_row).

std::istream&amp; operator &gt;&gt; (std::istream&amp; is, Grid_row&amp; grid_row) {
    std::size_t fin;
    int val;

    for (std::string term; is &gt;&gt; term; ) {
        try {
            val = std::stoi(term, &amp;fin);
            if (fin != term.size()) {
                std::cerr &lt;&lt; "Could not fully convert " + text + " to integer. Value was converted to " + text.substr(0, fin) + "." &lt;&lt; '\n';
            }
        } catch(const std::exception&amp; e) {
            std::cerr &lt;&lt; "Due to error: \"" + std::string{e.what()} +"\"; Could not convert " + text + " to integer. Value was converted to 0." &lt;&lt; '\n';
        }

        grid_row.push_back(val);

    }
    return is;
}


Now all you have is one try-catch, which is really all that is needed