Search This Blog

Wednesday 13 April 2016

Write a program with the following: A functin to read two double type numbers from keybord. A function to calculate divison 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.

 Practical 32




#include <iostream>

using namespace std;

class gajjar
{
    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()
{
    gajjar k;
    k.read();
    k.div();
    return 0;
}

No comments:

Post a Comment