Create User defined exception to check the following conditions and throw the exception if the criterion does not meet.
a. User has age between 18 and 55
b. User stays has income between Rs. 50,000 – Rs. 1,00,000 per month
c. User stays in Pune/ Mumbai/ Bangalore / Chennai
d. User has 4-wheeler
Accept age, Income, City, Vehicle from the user and check for the conditions mentioned
above. If any of the condition not met then throw the exception.
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int age;
double income;
char city[10];
long int vehicle;
cout<<"\nEnter the age: ";
cin>>age;
cout<<"\nEnter the income: ";
cin>>income;
cout<<"\nEnter the city: ";
cin>>city;
cout<<"\nEnter the vehicle(2/4): ";
cin>>vehicle;
try
{
if(age<18 || age>55)
throw age;
else
cout<<"\nAge is Perfect";
}
catch(int)
{
cout<<"\nException caught in age";
}
try
{
if(income<50000 || income>100000)
throw income;
else
cout<<"\nIncome is Perfect";
}
catch(double)
{
cout<<"\nException caught in Income";
}
try
{
if(strcmp(city,"pune") && strcmp(city,"mumbai") && strcmp(city,"bangalore") && strcmp(city,"chennai"))
throw city;
else
cout<<"\nCity is Perfect";
}
catch(char[])
{
cout<<"\nException caught in city";
}
try
{
if(vehicle!=4)
throw vehicle;
else
cout<<"\nVehicle is Perfect";
}
catch(long int)
{
cout<<"\nException caught in vehicle";
}
cout<<"\n\n";
return 0;
}
a. User has age between 18 and 55
b. User stays has income between Rs. 50,000 – Rs. 1,00,000 per month
c. User stays in Pune/ Mumbai/ Bangalore / Chennai
d. User has 4-wheeler
Accept age, Income, City, Vehicle from the user and check for the conditions mentioned
above. If any of the condition not met then throw the exception.
OUTPUT FOR THE PROGRAM |
#include<string.h>
using namespace std;
int main()
{
int age;
double income;
char city[10];
long int vehicle;
cout<<"\nEnter the age: ";
cin>>age;
cout<<"\nEnter the income: ";
cin>>income;
cout<<"\nEnter the city: ";
cin>>city;
cout<<"\nEnter the vehicle(2/4): ";
cin>>vehicle;
try
{
if(age<18 || age>55)
throw age;
else
cout<<"\nAge is Perfect";
}
catch(int)
{
cout<<"\nException caught in age";
}
try
{
if(income<50000 || income>100000)
throw income;
else
cout<<"\nIncome is Perfect";
}
catch(double)
{
cout<<"\nException caught in Income";
}
try
{
if(strcmp(city,"pune") && strcmp(city,"mumbai") && strcmp(city,"bangalore") && strcmp(city,"chennai"))
throw city;
else
cout<<"\nCity is Perfect";
}
catch(char[])
{
cout<<"\nException caught in city";
}
try
{
if(vehicle!=4)
throw vehicle;
else
cout<<"\nVehicle is Perfect";
}
catch(long int)
{
cout<<"\nException caught in vehicle";
}
cout<<"\n\n";
return 0;
}
Tags
CODING