Write a program in c++ for exception handling ,try block to detect and throw an exception if the condition “divide-by-zero” occurs .

                              EXCEPTION HANDLING


                             Exception handling in c++ one of the advantages of c++ over c in Exception handling. The exceptions are runtime anomalies or abnormal conditions that a program encounters during its executions. 




PROGRAM OF EXCEPTION HANDLING.


#include<iostream>
using namespace std;
class airthmetic
{
   double x,y;
   public:
      void accept()
      {
        cout<<"enter the two elements\n";
        cout<<"enter first element:";
        cin>>x;
        cout<<"enter the second element:";
        cin>>y;
      }
      void division()
      { double z;
        try
        {
          if(y==0)
            throw y;
          else
            {
            z=x/y;
            cout<<"division of the two nos. is:"<<z;
            }
        }
        catch(double a)
        {
           cout<<"the exeption is:"<<a;
        } 
      }
};
int main()
{
  airthmetic d;
  d.accept();
  d.division();
  return 0;
}

OUTPUT:
*******OUTPUT**********
enter the two elements
enter first element:2
enter the second element:2
division of the two nos. is:1
enter the two elements
enter first element:2
enter the second element:0
the exeption is:0

No comments:

Post a Comment

If you have any problems related to solutions or any concept please let me know.

Copyright (c) 2020 Custom Programs All Right Reserved

Pages