1. Accept two numbers a,b from the user and print a/b. If b is entered as zero, handle the exception and print appropriate message. Use, try, catch ,throw keywords
#include<iostream>
using namespace std;
int main()
{
double a, b;
do
{
cout << "\nPlease, enter a number a (0 - exit program): ";
cin >> a;
if (a == 0)break;
cout << "Please, enter a number b: ";
try
{
cin >> b;
if (b == 0)
throw "Division by zero!";
cout << "Result of division is " << a / b;
}
catch (const char* exception)
{
cerr << "Error: " << exception << endl;
}
} while (true);
}
Comments
Leave a comment