Exception Handling in C++



Exception are those errors which are occurred at the run time and are not supposed by the programmer.
An exception is a problem that arises during the execution of a program, such as an attempt to divide by zero.

In C++ there are two types of errors:
1.       Synchronus
2.       Asynchronus


Types of Exception

In C++ Exception Handling operation performed by the help of three blocks.

1.       Try – It contains those statements in which the error may be occurred.
2.       Throw – It throw the error to appropriate catch block.
3.       Catch – It handle the generated exceptions.

Exception Handling


Examples of Exception Handling
1.       C++ program to handle the exception divide by zero.
#include<iostream.h>
#include<conio.h>
void divide (int a, int b)
{
                                int c;
                                try                                          //try block in which the error may be occured
                                {
                                                if (b != 0)
                                                {
                                                                c = a/b;
                                                                cout<<”C = ”<<c;
                                                }
                                                else
                                                throw (b);           //throw block throws error to appropriate catch block
                                }
                                catch (int k)                        //catch block handle the generated exceptions
                                {
                                                cout<<”Error Caught”<<k;
                                }
                }
void main()
{
                clrscr();
                int m, n;
                cout<<”Enter First and Second Number = ”;
                cin>>m;
                cin>>n;
                divide (m, n);
                getch();
}
2.       C++ program to accept a number from the user and generate exception if number greater than 100.            
#include<iostream.h>
#include<conio.h>
void num (int a)
{
                                try
                                {
                                                if (a <= 100)
                                                {
                                                                cout<<”a = ”<<a;
                                                }
                                                else
                                                throw (a);
                                }
                                catch (int k)
                                {
                                                cout<<”Error Caught”<<k;
                                }
                }
void main()
{
                clrscr();
                int a;
                cout<<”Enter the Number = ”;
                cin>>a;
                num (a);
                getch();
}
               
3.       Use of Multiple Catch Statement
C++ program to handle the exception divide by zero (with condition (b ≠ 0) and (b > a) ).
#include<iostream.h>
#include<conio.h>
void divide (int a, int b)
{
                                int c;
                                try
                                {
                                                if (b == 0)             //throw if b is equal to zero
                                                throw (b);
                                                elseif (b > a)       //throw if b is greater than a
                                                else
{
                                                                c = a/b;
                                                                cout<<”C = ”<<c;
                                                }
                                }
catch (int k)
                                {
                                                cout<<”b is zero”<<k;
                                }
                                catch (char l)
                                {
                                                cout<<” b is greater than a”<<l;
                                }
                }
void main()
{
                clrscr();
                int m, n;
                cout<<”Enter First and Second Number = ”;
                cin>>m;
                cin>>n;
                divide (m, n);
                getch();
}


No comments:

Post a Comment