• Main Menu
  • Exception Handling


    There are two kinds of exceptions, namely, synchronous exceptions and asynchronous exceptions. Errors such as “out-of-range index” and “over-flow” belong to the synchronous type exceptions. The errors that are caused by events beyond the control of the program (such as keyboard interrupts) are called asynchronous exceptions. The proposed exception handling mechanism in C++ is designed to handle only synchronous exceptions.

    The purpose of the exception handling mechanism is to provide means to detect and report an exceptional circumstance or condition, so that appropriate action can be taken against it. The mechanism suggests a separate error handling code that performs the following tasks:

    • Find the problem (hit on the exception).
    • Inform that an error has occurred (throw the exception).
    • Receive the error information (catch the exception).
    • Take corrective actions (handle the exception).

    The error handling code basically consists of two segments; one to detect errors and to throw exceptions, and the other to catch the exceptions and to take appropriate action.

    Exception Handling Mechanism

    C++ exception handling mechanism is basically built upon three keywords, namely, try , throw, and catch . The keyword try is used to preface a block of statements (surrounded by braces) which may generate exceptions. This block of statements is known as try block. When an exception is detected, it is thrown using a throw statement in the try block. Catch blocks defined by the keyword catch, ‘catches’ the exception ‘thrown’ by the throw statement in the try block, and handles it appropriately.

    The catch block that catches an exception must immediately follow the try block that throws the exception. The general form of these two blocks is as follows:

    try { ------- throw exception; ------- } 
    catch(exception-type object) { ------- ------- }

    When a try block throws an exception, the program control leaves the try block and enters the catch statement of the catch block. Note that exceptions are objects used to transmit information about a problem. If the type of object thrown matches the argument- type in the catch statement, then catch block is executed for handling the exception. If it does not match, the program is aborted with the help of the abort() function which is invoked by default. When no exception is detected and thrown, the control goes to the statement immediately after the catch block, i.e., the catch block is skipped. This is shown in the program below:

    #include <iostream.h>
    void main() 
    {
        int a, b;
        cout << "\n Enter the value of a and b \n" ;
        cin >> a ;
        cout << "\n" ;
        cin >> b ;
        cout << "\n" ;
        try
        {
            if(x != 0)
            {
                cout << "Result(a/x) ="<< a/x > ;
            }
            else
            {
                trow(x);
            }
        }
        catch(int i)
        {
                cout <<" Exception caught : x = "<&ltx ;
        }
    }

    Output:

    Enter the value of a and b
    3
    
    3
    
    Exception caught : x = 0
    END

    The above program detects and catches a division-by-zero error. Different values of a and b will show how the exception mechanism works.

    Re-throwing an Exception

    A handler may decide to re-throw an exception caught without processing it. In such situations, we may simply invoke throw without any arguments as shown below:

    throw;

    This causes the current exception to be thrown to the next enclosing try/catch sequence and is caught by a catch statement listed after that enclosing try block. The following program demonstrates how an exception is re-thrown and caught.

    Example: Program to illustrate re-throwing of exception

    #include<iostream.h> 
    void divide(int x, double y) 
    { 
        cout<<"Inside function \n"; 
        try 
        { 
            if(y == 0.0) 
                throw y; 
            else 
                cout<<"Division = "<<x/y<<"n"; 
        } 
        catch(double) 
        { 
            cout<<"Caught double inside function \n"; 
            throw; 
        } 
        cout<<" END OF FUNCTION \n"; 
    } 
    void main() 
    { 
        cout<<"Inside main \n"; 
        try 
        { 
            divide(10.5,2.0); 
            divide(20.5,0.0); 
        } 
        catch(double) 
        { 
            cout<<"Caught doubling inside main \n"; 
        } 
        cout<<"End of main"; 
    }

    Output:

    Inside main
    Inside function
    Devision = 5
     END OF FUNCTION
    Inside function
    Caught double inside function
    Caught doubling inside main
    End of main

    When an exception is re-thrown, it will not be caught by the same catch statement or any other catch in that group. Rather, it will be caught by an appropriate catch in the outer try/ catch sequence only.

    A catch handler itself may detect and throw an exception. Here again, the exception thrown will not be caught by any catch statements in that group. It will be passed on to the next outer try/catch sequence for processing.

    Exception Specification

    It is possible to restrict a function to throw only certain specified exceptions. This is achieved by adding a throw list clause to the function definition. The general form of using an exception specification is:

    return-type function-name(argument-list) throw (type-list) 
    { 
        // function Body 
    }

    The type-list specifies the type of exceptions that may be thrown by the function. Throwing any other type of exception will cause abnormal program termination. If we wish to prevent a function from throwing any exception, we may do so by making the type-list empty. That is, we must use,

    throw(); // empty list

    Got Something To Say:

    Your email address will not be published. Required fields are marked *

    C
    174 queries in 0.534 seconds.