0
try
{
  int selection;

  if(selection > 4 || selection < 1)  
    throw selection;
}
catch(int selection)
{
   cout << "Menu selection out of range." << endl; 
}

The above code works fine for int values that are out of range, but I cannot get it to work if a char value is entered at (cin >> selection).

I have tried to post a catch block with an ellipsis [catch(...)] to account for char entries but that does not work.

I have also tried a catch block with [catch(char selection)] but that does not work either.

I would like to use an exception to handle this error as it would help in my overall project in other menu type areas.

Mat
  • 202,337
  • 40
  • 393
  • 406
user2825834
  • 1
  • 1
  • 1
  • 5
    You'll probably need to read it in as a string, and if you can't parse it as an int, throw an exception. – AbdullahC Sep 28 '13 at 08:07
  • 1
    I'd argue that this isn't a good use of an 'exception' - bad user input, particularly on `cin`, is something easily handled as a (bool?) return value, and implemented as part of 'normal' program flow. – Brett Hale Sep 28 '13 at 08:17
  • 1
    I don't understand what you want to achieve. Please post the non-working a code snippet with char, explain what it does, and explain what you'd need instead. – pts Sep 28 '13 at 08:36
  • Inappropriate user input, handled by exceptions, is too coarse IMO. While exceptions are for unexpected/exceptional cases, the user input, whatever it is (whatever UI allows), is an expected one. – jwaliszko Sep 28 '13 at 08:43
  • @OP There seems to be an aversion in some quarters for using exceptions for anything short of a nuclear explosion. Let me assure you that not everybody is quite so fastidious about this. – user207421 Sep 28 '13 at 09:00
  • @OP: Some people tend to consider exceptions as quite normal control flow mechanism... but why? And because bad user input is nearly the rule rather than exception, if you use exceptions for normal situations, how you would handle unusual ones? Another thing, have you ever had the possibility to debug program which which rises a few exceptions per second? Exceptions are not designed for that (it violates *principle of least astonishment*: http://c2.com/cgi/wiki?PrincipleOfLeastAstonishment) – jwaliszko Sep 28 '13 at 10:04

3 Answers3

0

You should try something with that snippet :

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

while (true) {
    cout << "Please enter a valid number: ";
    getline(cin, input);

    // This code converts from string to number safely.
    stringstream myStream(input);
    if (myStream >> myNumber)
        break;
    cout << "Invalid number, please try again" << endl;
}
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
0

You can make cin capture input using a string, and throw an exception if you can't parse it as an int. One way of doing it would be to use the str2int function from Dan Moulding's answer in the following manner:

std::string str;
cin >> str;

int selection;
STR2INT_ERROR err = str2int(selection, str.c_str());

try
{
  if(err != SUCCESS || selection > 4 || selection < 1)  
    throw str;
}
catch(std::string &selection)
{
   cout << "Menu selection [" << selection << "] invalid" << endl; 
}
Community
  • 1
  • 1
AbdullahC
  • 6,649
  • 3
  • 27
  • 43
0
{
    int a;
    try
    {
        cout << "Enter:";
        cin >> a;
        if (!a)
        {
            throw 1;
        }
        cout<<"Int";
    }
    catch (...)
    {
        cout << " no. Should be int" << '\n';
    }
}

be careful this code only work if input is char not if input is float