My goal is to have a simple piece of code that will raise an exception if input of non-int is entered.
My problem so far, is that I haven't been able to reach the catch block. I have read a little about bad_typeid, but do not know how to implement it.
#include <iostream>
#include <typeinfo>
using namespace std;
int isnum() //reads an int from user. Raises error if not of type int
{
int temp;
cout << "Please enter an int\n";
cin >> temp;
if (typeid(temp) != typeid(2)) //if input is not of same type as int
{
throw 4;
}
return temp;
}
int main()
{
try
{
int temp = isnum();
}
catch (int e)
{
cout << "An exception occurred. Exception Nr. " << e << endl;
}
return 0;
}
Thanks.