Wednesday, March 28, 2018

Write a C++ program for Exception Handling Divide by zero.


AIM: Write a C++ program for Exception Handling Divide by zero.
THEORY:
Exception handling is the process of responding to the occurrence, during computation, of exceptions – anomalous or exceptional conditions requiring special processing – often changing the normal flow of program execution. It is provided by specialized programming language constructs, computer hardware mechanisms like interrupts or operating system IPC facilities like signals.
In general, an exception breaks the normal flow of execution and executes a pre-registered exception handler. The details of how this is done depends on whether it is a hardware or software exception and how the software exception is implemented. Some exceptions, especially hardware ones, may be handled so gracefully that execution can resume where it was interrupted.
Alternative approaches to exception handling in software are error checking, which maintains normal program flow with later explicit checks for contingencies reported using special return values or some auxiliary global variable such as C's errno or floating point status flags; or input validation to preemptively filter exceptional cases.

SOURCE CODE:
#include<iostream>
using namespace std;
int main()
{
            int a,b,c;
            float d;
            cout<<"\n Enter value of a:";
            cin>>a;
            cout<<"\n Enter value of b:";
            cin>>b;
            cout<<"\n Enter value of c:";
            cin>>c;
            try
            {
                        if((a-b)!=0)
                        {
                                    d=c/(a-b);
                                    cout<<"\n Result is"<<d;
                        }
                        else
                        {
                                    throw (a-b);
                        }
            }
            catch (int i)
            {
                        cout<<"Ans is infinte because a-b is"<<i;
            }
            return 0;
}

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home