TASK: Add an exception handler (try/catch/throw) to the class that throws an error message (e.g. “value out of range”) in the getValue function that is caught and handled in the main program. Implement two version of the program. In version 1, throw the error in getValue, catch the error in the main, display an error message in the main, and then allow the program to terminate. In the second version, perform the same basic actions (throw and catch) but keep re-invoking the getValue function from the main program until the user enters a valid value. // Starter Code class subRange { public: subRange( int, int ); int getValue( ); private: int lower, upper; }; subRange::subRange( int low, int high ) { lower = low; upper = high; } int subRange::getValue() { int v; cout << "Enter value [ " << lower << ", " << upper << " ]: "; cin >> v; return v; } void main() { subRange x(1,10); cout << x.getValue( ) << endl; }

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

The following program contains a simple class that is designed to allow a user to enter an integer value within a specific range. The code prompts the user with a message that explains the limits of the valid range of values, but it does not force the user to enter legal values. We have examined a number of approaches for dealing with this problem. One approach is to place an assert statement in the getValue function to terminate the program when an illegal input occurs. An alternative approach is to put a loop inside the getValue function that prompts the user repeatedly until a valid value is received. Neither of these approaches is satisfactory, because they force the user to adopt a specific approach to dealing with the error. If you compare the execution of a program with the operation of a business, you will notice that both consist of a hierarchy of classes and functions. In a business, we expect the people in the upper layers of the company (managers) to set policies and the people in lower positions (workers) to implement the policy. We want the same type of behavior in our programs, but both of our approaches to handling errors lead to an inversion of authority -- the bottom layer (implementation of the class) is making decisions for the top-layer (user’s application program). A better approach to managing this problem is to use an exception handler that passes control back to a top-level function when an error occurs so the user can make the decision to terminate the program or continue processing. 

 

TASK: Add an exception handler (try/catch/throw) to the class that throws an error message (e.g. “value out of range”) in the getValue function that is caught and handled in the main program. Implement two version of the program. In version 1, throw the error in getValue, catch the error in the main, display an error message in the main, and then allow the program to terminate. In the second version, perform the same basic actions (throw and catch) but keep re-invoking the getValue function from the main program until the user enters a valid value. // Starter Code class subRange { public: subRange( int, int ); int getValue( ); private: int lower, upper; }; subRange::subRange( int low, int high ) { lower = low; upper = high; } int subRange::getValue() { int v; cout << "Enter value [ " << lower << ", " << upper << " ]: "; cin >> v; return v; } void main() { subRange x(1,10); cout << x.getValue( ) << endl; }

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 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