Thursday 20 October 2016

Exception Handling

/*
CREATE A USER DEFINED EXEPTION TO CHECK THE EXEPTION AND CHECK THE FOLLOWING CONDITIONS AND THROW EXEPTION IF CRITERIA DOES NOT MATCH
A] USER HAS AGE BETWEEN 18 AND 55
B]USER STAYS IN PUNE ,MUMBAI ,BANGLORE ,CHENNAI.
C]USER HAS INCOME BETWEEN 50000 AND 100000
D] USER HAS 4 WHEELER
*/



#include<iostream>
using namespace std;


class sample
{
public:
int age,income;
char ch;
string city;
public:
void getage()
{
cout<<"enter age\n";
cin>>age;
}
void getincome()
{
cout<<"enter income\n";
cin>>income;
}
void getcity()
{
cout<<"enter city\n";
cin>>city;
}
void getveh()
{
cout<<"do you have a 4 wheeler ? (y/n)\n";
cin>>ch;
}
};


int main()
{
sample p;
p.getage();
p.getcity();
p.getincome();
p.getveh();


try
{
if((p.age<18) || (p.age>55))
{
throw p;
}
else
{
cout<<"the age is "<<p.age<<endl;
}
}
catch(sample p)
{
cout<<" age exception caught"<<endl;
}


try
{
if(p.city=="mumbai"||p.city=="pune"||p.city=="delhi"||p.city=="banglore"||p.city=="chennai")
{
cout<<"user stays in the given city"<<endl;
}
else
{
throw p;
}
}
catch(sample p)
{
cout<<"exception for city occured"<<endl;
}


try
{
if((p.income<50000)||(p.income>100000))
{
throw p;
}
else
{
cout<<"the income is "<<p.income<<endl;
}
}
catch(sample p)
{
cout<<"exception for income caught"<<endl;
}


try
{
if(p.ch=='y')
{
cout<<"the user has four wheeler"<<endl;
}
else
{
throw p;
}
}
catch(sample p)
{
cout<<"exception for vehicle cuaght"<<endl;
}
}

No comments:

Post a Comment