exception handling in c++

/*
Crete 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<stdexcept>
#include<typeinfo>    //header file for type operation of RTTI(return type type
                                       identification)
#include<string>
#include<cstring>
using namespace std;
int main()
{
    int i,j,n,age;
    char car;
    double income;
    char city[20];
   
    cout<<"\nEnter age:";
    cin>>age;
    try
    {
        if(age<18||age>55)
        throw runtime_error("\nEnter proper age");
    }
    catch(const exception &e)
    {
        cout<<"\nException is="<<e.what();
        //WHAT = it is the member function for type recognation used as type info
        //it invoke proper datatype of any variable at runtime.
    }
   
    cout<<"\nEnter your income:";
    cin>>income;
    try
    {
        if(income<500000||income>100000)
        throw runtime_error("\nInvalid income");
    }
    catch(const exception &e)
    {
        cout<<"\nException is="<<e.what();
    }
   
    cout<<"\nIn which city do you live?:";
    cin>>city;
    try
    {
        if(strcmp(city,"pune")&&strcmp(city,"mumbai")&&strcmp(city,"chennai")&&strcmp(city,"banglore"))
        throw runtime_error("\nEnter proper city");
    }
    catch(const exception &e)
    {
        cout<<"\nException is="<<e.what();
    }
   
    cout<<"\ndo you own a car or not(y/n):";
    cin>>car;
    try
    {
        if(car=='n')
        throw runtime_error("\nuser not own a car");
    }
    catch(const exception &e)
    {
        cout<<"\nException is="<<e.what();
    }
   
    cout<<"\ncomplete info. is as follows:";
    cout<<"\nage is="<<age;
    cout<<"\nincome is="<<income;
    cout<<"\ncity is="<<city;
    cout<<"\nuser have a car"<<car;
}

output-
 Enter age:18

Enter your income:5000

Exception is=
Invalid income
In which city do you live?:mumbai

do you own a car or not(y/n):n

Exception is=
user not own a car
complete info. is as follows:
age is=18
income is=5000
city is=mumbai
user have a car=n

Comments

Popular posts from this blog

Write a function template selection Sort. Write a program that inputs, sorts and outputs an integer array and a float array.

C++ program that creates an output file, writes information to it, closes the file and open it again as an input file and read the information from the file.

Write C++ program using STL for Sorting and searching