Friday, 26 January 2018

Write a program with the following: A function to read two double type numbers from keyboard. A function to calculate division of these two numbers. A try block to throw an exception when a wrong type of data is keyed in . A try block to detect and throw an exception if the condition “divide by zero ” occurs. Appropriate catch block to handle the exception thrown.

#include <iostream>

using namespace std;

class exe
{
    double a,b;
    public:
    void read()
    {
        cout<<"\nEnter two double type numbers:";
        cin>>a>>b;
    }
    void div()
    {
        try{

            if(cin.fail())
                throw "Bad input!";
            if( b == 0 )
            throw 0;

            cout<<"\nAns is "<<a/b;
        }
        catch(const int n)
        {
            cout << "\nDivision by " << n << " not allowed\n";
        }
        catch(const char* Str)
        {
            cout<< Str;
        }
    }
};

int main()
{
    exe ex;
    ex.read();
    ex.div();
    return 0;
}

No comments:

Post a Comment

It's time To increase blogging capability. To have a chance to contribute in digital world. Any Interested People who want to make t...