Saturday, 28 September 2013

how to create exception for char when the input should be int

how to create exception for char when the input should be int

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.

No comments:

Post a Comment