Show a conditional expression that returns either a double or an int. Provide an int catch handler and a double catch handler. Show that only the double catch handler executes regardless of whether the int or the double is returned.
#include <iostream>
using namespace std;
//function to execute conditional expression
double Number_exception(int n1,int x,double n2,double y)
{
if(n1>x)
{
return n1;
}
else
{
return x;
}
if(n2>y)
{
return n2;
}
else
{
return y;
}
}
int main() {
int n1,x;
double n2,y;
//n1 is the value to check with x
cout<<"Enter value of n1:\n";
cin>>n1;
cout<<"Enter value of x:\n";
cin>>x;
//n2 is the value to check with y
cout<<"Enter value of n2:\n";
cin>>n2;
cout<<"Enter value of y:\n";
cin>>y;
//calling a function to execute conditional expression
double result= Number_exception(n1,x,n2,y);
//try block
try{
throw result; //throwing a exception
}
catch(int ex) //int type exception caught
{
cout<<"int exception caught";
}
catch(double ex) // //double type exception caught
{
cout<<"double exception caught";
}
}
Output::
Note:: In case you have any problem please comment in comment section.I will definately solve your problems.
Get Answers For Free
Most questions answered within 1 hours.