CS Electrical And Electronics
@cselectricalandelectronics

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?
CS Electrical And Electronics Staff asked 4 years ago

I need short information.

1 Answers
CS Electrical And Electronics Staff answered 4 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