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;
}
}
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;
}