Home Uncategorized Write a Program for Exception Handling Divide by zero

Write a Program for Exception Handling Divide by zero


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


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.


using namespace std;
int main()
    int a,b,c;
    float d;
    cout<<“Enter value of a: “;
    cout<<“Enter value of b: “;
    cout<<“Enter value of c: “;
            cout<<“Result is: “<<d<<endl;
            throw (a-b);
    catch (int i)
        cout<<“Answer is undefined because a-b is: “<<i<<endl;
    return 0;


Enter a value: 15
Enter b value: 15
Enter c value: 25
Answer is undefined because a-b is: 0

Enter a value: 3
Enter b value: 1
Enter c value: 6
Result is: 3

Back to Programs.