C++ program to handle Exception when a number is divided by zero

#include <iostream>
using namespace std;
int main()
{
    int divisor,quotient;
    try
    {
        cout<<"Enter divisor: ";
        cin>>divisor;
        if(divisor!=0)
        {
            quotient=1000/divisor;
            cout<<"Quotient="<<quotient;
        }
        else
            throw divisor;
    }
    catch(int d)
    {
        cout<<"Divisor can not be zero,";
    }
    return 0;
}

No comments:

Post a Comment