Wednesday, October 12, 2016

Multiple Catch Statements (Solution To E Balagurusamy Problem 13.2)

Tags

In Multiple Catch Statements The Type of Argument is Matched with all catch statements and after a match the statement after the last catch block is executed.

Problem:
Write A Program that illustrates the application of multiple catch statements.

Solution:
#include<iostream>
using namespace std;
void test()
{
int x;
cout<<"Enter Any Positive Value Except 0\n";
cin>>x;
if(x<0)
{
throw (float)x;
}
else if(x==0)
{
throw x;
}
cout<<"\nEntered Value is "<<x;
}
main()
{
try
{
test();
}
catch(float)
{
cout<<"\nNegative Values Not Allowed";
}
catch(int)
{
cout<<"\n0 Not Allowed";
}
cout<<"\nEnd";
}
Output: