Handling Exception

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include <exception>

using namespace std;

int getInt(string prompt)
{
	int integer;
	cout << prompt;
	if(!(cin >> integer))
	{
		runtime_error e("Non-integer data");
		throw e;
	}
	return integer;
}

int main()
{
	int age;
	try
	{
		age = getInt("Enter your age");
	}
	catch (runtime_error e)
	{
		cout << e.what() << endl;
		cout << "Age is being set to 0" << endl;
		age = 0;
	}
		
	cout << "Age is " << age << endl;
	return 0;
}


Attached is a sample code from my book I currently reading, but the code was unable to compile. Wondering where went wrong.
You need #include <stdexcept>
Topic archived. No new replies allowed.