Objectives 1Utilize C exceptions by athrowing bcatching 2Cre
Objectives:
1.Utilize C++ exceptions by
a)‘throw’ing
b)‘catch’ing
2.Create a new class for the purpose of exceptions
Question:
When setting the dimensions of a geometric object, the lengths of the sides should be positive values. Write a program that repeatedly asks for the dimensions of a rectangle and displays the area. Before an error is presented, collected both the length and the width values in a new class called Rectangle.
Create a class DimError, that is used by the new Rectangle class. The following methods must be defined for the Rectangle object:
void Rectangle::setWidth(int width);
void Rectangle::setLength(int length);
If either setWidth or setLength is called with a negative value it throws a DimError that includes a message indicating which method was called and the reason for the error. Use the main function defined below to produce the output that follows.
Main Function:
int main(void) {
Rectangle r;
try {
while (true) {
int l, w;
cout << \"Enter a length: \";
cin >> l;
cout << \"Enter a width: \";
cin >> w;
r.setLength(l);
r.setWidth(w);
cout << \"The area is: \" << r.area() << endl;
}
} catch (DimError de) {
cout << \"Couldn\'t set the rectangle\'s dimensions: \" << endl
<< de.what() << endl;
}
cout << \"Exiting...\" << endl;
return 0;
}
Output:
Enter a length: 5
Enter a width: 10
The area is: 50
Enter a length: 3
Enter a width: 3
The area is: 9
Enter a length: -3
Enter a width: 5
Couldn\'t set the rectangle\'s dimensions:
setLength called with a negative length.
Exiting...
Press any key to continue . . .
Solution
Please follow the code and comments for description :
CODE :
#include <cstdlib> // required header files
#include <iostream>
#include <string>
#include <stdexcept>
#include <exception>
using namespace std;
class DimError { // class that has the exception defined
public:
DimError(const string& msg) : message(msg) { // empty constructor
}
string what() const { // get the message fromt the user
return (message);
}
private:
string message; // variable that holds the message
};
class Rectangle { // class that takes in the the dimesnions of the rectangle
int LengthData, WidthData, areaOfRect;
public:
void setLength(int length) { // method that takes the length
if (length < 0) { // check for the data
throw DimError(\"setLength called with a negative length.\"); // throw the exception
} else {
LengthData = length; // else data
}
}
void setWidth(int width) { // method that takes the width
if (width < 0) { // check for the data
throw DimError(\"setWidth called with a negative width.\"); // throw the exception
} else {
WidthData = width; // else data
}
}
double area() { // method that returns the result as area
areaOfRect = WidthData * LengthData; // code to calculate the area
return areaOfRect; // return area
}
};
int main(void) { // driver method
Rectangle r; // object for the class
try { // try catch block
while (true) { // iterate tiil teh user exits
int l, w; // local variables
cout << \"Enter a length: \"; // prompt
cin >> l; // get data
cout << \"Enter a width: \"; // prompt
cin >> w; // get data
r.setLength(l); // call the functions
r.setWidth(w);
cout << \"The area is: \" << r.area() << endl; // print data to console
}
} catch (DimError& de) { // catch the error
cout << \"Couldn\'t set the rectangle\'s dimensions: \" << endl << de.what() << endl; // print the message
}
cout << \"Exiting...\" << endl; // exit prompt
return 0;
}
OUTPUT :
Enter a length: 5
Enter a width: 10
The area is: 50
Enter a length: 3
Enter a width: 3
The area is: 9
Enter a length: -3
Enter a width: 5
Couldn\'t set the rectangle\'s dimensions:
setLength called with a negative length.
Exiting...
Hope this is helpful.


