Create an exception Class called NegativeAge and an exception class named AgeTooHigh. Each of these two exception classes should have an integer member variable to hold the age. Each of these exception classes should also have a constructor to receive and set the age, and a getter function to get the age. See page 983-984 in the 8th edition text for an example. A screen shot of these pages are attached on the next page in these instructions under "extracting data from the Exception Class". Then use your class in a try-catch block. The try block should call a function named “int getAge()" that gets a user's age using a prompt and cin. The function getAge() throws the NegativeAge exception if the age provided by the user is below zero, and the AgeTooHigh exception if the age is above 120. getAge() will use the correct exception class to throw the exception and it will pass the age back to the catch block, using the exception and the exception class's age member variable.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

See attached images

Create an exception Class called NegativeAge and an exception class named AgeTooHigh.
Each of these two exception classes should have an integer member variable to hold the age. Each of these exception
classes should also have a constructor to receive and set the age, and a getter function to get the age. See page 983-984 in
the 8th edition text for an example. A screen shot of these pages are attached on the next page in these instructions under
"extracting data from the Exception Class".
Then use your class in a try-catch block.
The try block should call a function named “int getAge()" that gets a user's age using a prompt and cin. The function
getAge() throws the NegativeAge exception if the age provided by the user is below zero, and the AgeTooHigh exception
if the age is above 120. getAge() will use the correct exception class to throw the exception and it will pass the age back
to the catch block, using the exception and the exception class's age member variable.
Transcribed Image Text:Create an exception Class called NegativeAge and an exception class named AgeTooHigh. Each of these two exception classes should have an integer member variable to hold the age. Each of these exception classes should also have a constructor to receive and set the age, and a getter function to get the age. See page 983-984 in the 8th edition text for an example. A screen shot of these pages are attached on the next page in these instructions under "extracting data from the Exception Class". Then use your class in a try-catch block. The try block should call a function named “int getAge()" that gets a user's age using a prompt and cin. The function getAge() throws the NegativeAge exception if the age provided by the user is below zero, and the AgeTooHigh exception if the age is above 120. getAge() will use the correct exception class to throw the exception and it will pass the age back to the catch block, using the exception and the exception class's age member variable.
Extracting Data from the Exception Class
Sometimes we might want an exception object to pass data back to the exception handler.
For example, suppose we would like the Rectangle class not only to signal when a nega-
tive value has been given, but also to pass the value back. This can be accomplished by
giving the exception class members in which data can be stored.
In our next modification of the Rectangle class, the Negativewidth and NegativeLength
classes have been expanded, each with a member variable and a constructor. Here is the
code for the Negativewidth class:
class Negativewidth
private:
double value;
· 16 Exceptions, Templates, and the Standard Template Library (STL)
public:
Negativewidth(double val)
( value = val; }
double getvalue() const
{ return value; }
};
When we throw this exception, we want to pass the invalid value as an argument to the class's
constructor. This is done in the setwidth member function with the following statement:
throw Negativewidth(w);
This throw statement creates an instance of the Negativewidthclass and passes a copy of the
w variable to the constructor. The constructor then stores this number in Negativewidth's
member variable, value. The class instance carries this member variable to the catch block
that intercepts the exception.
In the catch block, the value is extracted with code such as
catch (Rectangle::Negativewidth e)
cout « "Error: " « e.getValue()
« " is an invalid value for the"
« " rectangle's width.\n";
Notice that the catch block defines a parameter object named e. This is necessary because
we want to call the class's getvalue function to retrieve the value that caused the exception.
Transcribed Image Text:Extracting Data from the Exception Class Sometimes we might want an exception object to pass data back to the exception handler. For example, suppose we would like the Rectangle class not only to signal when a nega- tive value has been given, but also to pass the value back. This can be accomplished by giving the exception class members in which data can be stored. In our next modification of the Rectangle class, the Negativewidth and NegativeLength classes have been expanded, each with a member variable and a constructor. Here is the code for the Negativewidth class: class Negativewidth private: double value; · 16 Exceptions, Templates, and the Standard Template Library (STL) public: Negativewidth(double val) ( value = val; } double getvalue() const { return value; } }; When we throw this exception, we want to pass the invalid value as an argument to the class's constructor. This is done in the setwidth member function with the following statement: throw Negativewidth(w); This throw statement creates an instance of the Negativewidthclass and passes a copy of the w variable to the constructor. The constructor then stores this number in Negativewidth's member variable, value. The class instance carries this member variable to the catch block that intercepts the exception. In the catch block, the value is extracted with code such as catch (Rectangle::Negativewidth e) cout « "Error: " « e.getValue() « " is an invalid value for the" « " rectangle's width.\n"; Notice that the catch block defines a parameter object named e. This is necessary because we want to call the class's getvalue function to retrieve the value that caused the exception.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY