PART A

Modify the code below to try, throw and catcherrors the following values with the specified data types. Print a statement, similar to the "Catch Integer" example in the code, when the exception is caught.

1) long 8
2) double 4.9
3) char 'A'
4) string "Test Result"
#include < iostream>
using namespace std;
int main()
{
//try Block
try
{
throw(int(1)); //catch(int)
}
//Handlers
catch (int x)
{
cout << "Catch Integer " << x << endl;
}
}

PART B

Modify the code below to try, throw and catch errors if the values entered for donuts and milk is less than zero. Print a statement, similar to the existing example in the code, when the exception is caught.

#include < iostream>
using namespace std;
int main()
{
int donuts, milk;
double dpg;
try
{
cout << "Enter number of donuts:\n";
cin >> donuts;
cout << "Enter number of glasses of milk:\n";
cin >> milk;
if (milk <= 0)
throw donuts;
dpg = donuts/static_cast (milk);
cout << donuts << " donuts.\n"<< milk << " glasses of milk.\n"<< "You have " << dpg<< " donuts for each glass of milk.\n";
}
catch(int e)
{
cout << e << " donuts, and No Milk!\n"<< "Go buy some milk.\n";
}
cout << "End of program.\n";return 0;
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.