Program To Display Arithmetic Operations On Two Numbers Using Try Catch :
#include<iostream>
using namespace std;
void operations()
{ int a,b;
char c;
cout<<"Enter Two Values\n";
cin>>a>>b;
cout<<"Enter Any Arithmentic Operator";
cin>>c;
try
{
if(c!='+'&&c!='*'&&c!='/'&&c!='-')
{
throw a;
}
else
{
switch(c)
{
case '+': cout<<"Sum is "<<a+b; break;
case '-': cout<<"Difference is "<<a-b; break;
case '*': cout<<"Product is "<<a*b; break;
case '/': cout<<"Quotient is "<<a/b; break;
}
}
}
catch(int)
{
cout<<"Invalid Operator";
}
}
main()
{
operations();
}
Output:
#include<iostream>
using namespace std;
void operations()
{ int a,b;
char c;
cout<<"Enter Two Values\n";
cin>>a>>b;
cout<<"Enter Any Arithmentic Operator";
cin>>c;
try
{
if(c!='+'&&c!='*'&&c!='/'&&c!='-')
{
throw a;
}
else
{
switch(c)
{
case '+': cout<<"Sum is "<<a+b; break;
case '-': cout<<"Difference is "<<a-b; break;
case '*': cout<<"Product is "<<a*b; break;
case '/': cout<<"Quotient is "<<a/b; break;
}
}
}
catch(int)
{
cout<<"Invalid Operator";
}
}
main()
{
operations();
}
Output: