Need help ! 16.4.2 a. Write the declaration of a user-defined exception type nam
ID: 3761731 • Letter: N
Question
Need help !
16.4.2 a. Write the declaration of a user-defined exception type named BadData.
b. Write a void function GetAge that prompts for and inputs the user's age (type int) from the keyboard.
The function returns the user's age through the parameter list unless the input value is a negative
number, in which case a BadData exception id thrown.
16.4.3. Write a try-catch statement that calls the GetAge function of Question 16.4.2b. If a BadData exception
is thrown, print error message and rethrow the exception to a caller; otherwise, execution should just
continue.
Explanation / Answer
16.4.2 a
class BadData : public exception{}
16.4.2 b
void GetAge()
{
int age;
while(true)
{
cout << "Enter age: ";
cin >> age;
if(age < 0)
throw new BadData();
}
}
16.4.3 b
try
{
GetAge();
}
catch(BadData ex)
{
cout << "Negative age is entered." << endl;
throw new BadData();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.