Saturday, February 2, 2013

Exception Handling in C++


Exception Handling:
Those errors comes in program at run time is known as exception. To manage these exceptions we create procedure, this is known as exception handling.

Advantages of exception handling:
                                                        i.            We can trace the error.
                                                      ii.            It makes error free program.
                                                    iii.            Exception handling helps to detect program run time error.

Note:  This feature of  C++ is not supported in Borland Trubo C++

Try Block:
The keyword try contains the block of statements surrounded by braces, which may generated and this block of statement is known try block.

Catch Block:
This block catch the exception (handle) generated in try block, it is thrown in the catch block.

Throw statement:
When exception is detected, the throw statement sends the control to catch block.
(The throw statement exists in try block. When throw statement execute, it pass control to catch block.)






#include <iostream.h>
#include<conio.h>
class Error
{
            public:
            void devide(int x, int y)
            {
                        If(y!=0)
                        {
                                    int Result;
                                    Result=x/y;
                                    cout<<”Result ”<<Result;
                        }
                        else
                        {
                                    throw(y);
                        }
            }
};




void main()
{
            try
            {
                        Error e1;
                        e1.divide(10,0);
            }
            catch(int errr)
            {
                        cout<<”Ececution ”<<err;
            }
}

Specifying Exception:
Specifying Exception and multiple catch blocks:
It is possible to restrict a function to throw only specific exceptions. This is done by specific throw list to function definition.

Note:- If throw statement do not have any list then this function do not throw any exception.
void test(int x) throw(int, double)
{
if(x==0)
            throw ‘x’;
else if(x==1)
            throw 1;
else if(x==-1)
            throw 1.5;
}
void main()
{
            Try
{
            cout<<”Test throw statement”;
            test(0);
            test(1);
            test(-1);
}
catch(int i)
{
            cout<<”Integer exception”;
}
catch(char i)
{
            cout<<”Character exception”;
}
catch(double i)
{
            cout<<”Double exception”;
}
}

Rethrow Exception:
When a throw exception generate an exception and transfer the control to outer try catch block.
void divide(int x,int y)
{
            try
            {
                        if(y==0)
                        {
            throw(y);
}
else
{
            cout<<”Division : ”x/y;
}
}
catch(int)
{
            throw;
}
}
void main()
{
try
            {
divide(10,2);
divide(20,0);
}
catch(int)
{
            cout<<”Exception generate”;
}
cout<<”End of try catch”;
}

0 comments:

Post a Comment

Powered by Blogger.