Question: This exercise is the same as Programming Exercise 2, except that the exceptions should be derived from a base class (itself derived from logic_error) that

This exercise is the same as Programming Exercise 2, except that the exceptions should be derived from a base class (itself derived from logic_error) that stores the two argument values, the exceptions should have a method that reports these values as well as the function name, and a single catch block that catches the base-class exemption should be used for both exceptions, with either exception causing the loop to terminate.

Data from Programming Exercise 2

Modify Listing 15.11 so that the two exception types are classes derived from the logic_error class provided by the header file. Have each what() method report the function name and the nature of the problem. The exception objects need not hold the bad values; they should just support the what() method.

Listing 15.11 error4.cpp //error4.cpp - using exception classes #include #include // or

math.h, unix users may need -1m flag #include "exc_mean.h" // function prototypes

Listing 15.11 error4.cpp //error4.cpp - using exception classes #include #include // or math.h, unix users may need -1m flag #include "exc_mean.h" // function prototypes double hmean (double a, double b); double gmean (double a, double b); int main() { using std::cout; using std::cin; using std::endl; double x, y, z; cout < < "Enter two numbers: "; while (cin >> x >> y) { try { // start of try block z hmean (x,y); cout < < "Harmonic mean of " < < x < < " and " < < y < < " is " < < Z < < endl; cout < < "Geometric mean of " < < x < < " and " < < y " is " < < gmean (x, y) < < endl; cout < < "Enter next set of numbers : "; }// end of try block catch (bad_hmean & bg) { bg.mesg(); cout < < "Try again. "; // start of catch block

Step by Step Solution

3.36 Rating (162 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

include include or mathh unix users may need lm flag in... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Introduction Java Program Questions!