Well...
1) you aren’t providing a compilable example which reproduces your error;
2) you aren’t describing your problem;
3) you’re asking only if you’re doing something wrong or not, so the proper answer to your question would be “Yes, you are” - period;
4) you are likely to be using “using namespace std;” (but this is a debatable point);
5) you’re throwing a std::string and catching an int.
What shouldn’t I use exceptions for?
...
In particular, do not use exceptions for control flow. throw is not simply an alternative way of returning a value from a function (similar to return). Doing so will be slow and will confuse most C++ programmers who are rightly used to seeing exceptions used only for error handling. Similarly, throw is not a good way of getting out of a loop. https://isocpp.org/wiki/faq/exceptions
Something like this, perhaps:
1 2 3 4 5 6 7 8 9 10 11 12 13
// accept a line containing [1,max_char] characters from stdin
std::string get_line_n( std::size_t max_chars )
{
static std::string line ;
std::getline( std::cin, line ) ;
if( line.empty() ) std::cout << "enter at least one character." ;
elseif( line.size() > max_chars ) std::cout << "enter no more than " << max_chars << " characters." ;
elsereturn line ;
std::cout << " try again: " ;
return get_line_n(max_chars) ;
}