User defined Exception

Description:

Write a cpp program to Handle User defined Exceptions such as Divide by zero. Test Case 1 Input (stdin) 10 0 Expected Output Divide by zero exception Test Case 2 Input (stdin) 15 5 Expected Output 3

Program :

#include <iostream>  

#include <exception>  

using namespace std;  

class MyException : public exception{  

    public:  

        const char * what() const throw()  

        {  

            return "Divide by zero exception\n";  

        }  

};  

int main()  

{  

    try  

    {  

        int x, y;  

       // cout << "Enter the two numbers : \n";  

        cin >> x >> y;  

        if (y == 0)  

        {  

            MyException z;  

            throw z;  

        }  

        else  

        {  

            cout << " " << x/y << endl;  

        }  

    }  

    catch(exception& e)  

    {  

        cout << e.what();  

    }  

}