Write a Cpp program to read three values, subtract them, divide onle value by subtracted value, if result is zero then throw exception, else display. Use exception with multiple catch block?

All QuestionsCategory: Cpp LanguageWrite a Cpp program to read three values, subtract them, divide onle value by subtracted value, if result is zero then throw exception, else display. Use exception with multiple catch block?
Chetan Shidling Staff asked 5 years ago

I need short information.

1 Answers
Chetan Shidling Staff answered 5 years ago

Code:

#include <iostream>
using namespace std;
void divide(float a, float b, float c)
{
float x = a-b-c;
if(x !=0 )
{
cout<<“Result (c/x) = ” << c/x << “\n”;
}
else
{
throw(x);
}
}
int main()
{
float a, b, c, d, e, f;
try
{
cout<<“Enter the values of a, b, c, d, e, f:”;
cin>>a>>b>>c>>d>>e>>f;
divide(a, b ,c);
divide(d, e, f);
}
catch(float i)
{
cout<<“Exception caught Float : DIVIDE BY ZERO”;
cout<<“\nEND” << endl;
}
catch(int i)
{
cout<<“Exception caught Integer : DIVIDE BY ZERO”;
cout<<“\nEND” << endl;
}
catch(double i)
{
cout<<“Exception caught Double : DIVIDE BY ZERO”;
cout<<“\nEND” << endl;
}
}

Output:

Enter the values of a, b, c, d, e, f:20
10
5
20
10
10
Result (c/x) = 1
Exception caught Float : DIVIDE BY ZERO
END