Click Here!
home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


The catch-all catch clause must be the last catch clause among multiple catch clauses. This catch clause will catch any exception thrown. I have seen application code in which every try block is followed by a catch-all catch clause. In my opinion, this style of exception handling should not be implemented. The exceptions to be caught should be explicitly stated. The catch-all catch clause should be used only in situations in which you do not specifically know the type of exceptions that might be thrown.

Let’s examine the function definitions that follow the main function. The majority of these function definitions are rather trivial, so I will move through the descriptions quickly.

The first function, dbInit(), pretends to initialize a database and simply returns a success value. Next is the function dbOpen() that fakes database open functionality; it simply returns a positive, non-zero handle to the calling function. Moving the source file, you come upon the next function, dbGetData(). This function provides some functionality, even if minimal; it simply assigns each ASCII digit, 0–9, to the character array passed in (actually a pointer). Finally, the function returns a success value to the calling function. You arrive at the function dbClose(), which simulates the closing of a database file; it merely returns a success value.

The previous set of function definitions represents the traditional style of a function returning a result value. Although these functions return only a simple value to designate success or failure, the code required at the call site can become tedious and excessive. You can witness this with just the minimal code in this How-To.

The next set of function definitions is different than the previous set. One function within this set uses the third component of exception handling, the throw statement.

void dbGetDataEH( int field, char *data )
{
    int ret = DB_ACCESS_ERROR ;

    throw( ret ) ;
}

Move forward through the code to the dbGetDataEH() function definition. Notice that an error code mnemonic is assigned to the ret integer variable. The next line of code in the function is a throw statement. The argument supplied to the throw statement is an int variable. You can supply any data type, either simple or user defined. This function is called within the try block within the main function. Because this function throws an exception, control is immediately transferred to the catch clause that follows:

    dbGetDataEH( 1, data ) ;
    cout << “Data is: ” << data << endl ;
    dbCloseEH( theHandle ) ;
}
catch( int v ) {
    cout << “Error with DB: ” << v << endl ;
}

This exception handler manages exceptions of type int.

If a statement raises an exception, any statements that follow will not be executed. In this example, the cout and the dbCloseEH() call are not executed.

Comments

This How-To has taken a quick look at the basics of exception handling. With these basics, you can begin to reap the benefits of exception handling.

An additional benefit of exception handling is a more logical grouping of code. Developers tend to group logical sections of code within try blocks, thereby producing source code that is more readable, robust, and maintainable.

The How-Tos that follow will delve into more detail concerning exception handling.

11.2 Use the various catch constructs such as multiple catch clauses, catch ordering, and the rethrowing of exceptions?

Problem

I understand that multiple catch clauses can be provided to handle exceptions thrown from a try block. I am unsure if ordering of catch clauses is important and want to know if an exception caught can be rethrown by a catch clause.

Technique

The technique required to organize multiple catch clauses is quite easy. The tricky part is to understand any exceptions that require handling.

The following source code demonstrates how to apply the use of multiple catch clauses, including the catch-all clause. The technique required to rethrow exceptions is also shown.

Steps

1.  Change to your base source directory and create a new directory named CATCH. Next, start your code editor and type in the following source code:
// catch.cpp - demonstrates the use
// of multiple catch clauses
#include <exception>
#include <iostream>
using namespace std ;

void rethrowFunc( int i ) ;
void throwFunc( int i ) ;

int main( )
{
    for( int i = 0; i < 6; i++ )
    {
        try {
            rethrowFunc( i ) ;
        }
        catch( int v ) {
            cout << “Int caught: ” << v << endl ;
        }
        catch( float f ) {
            cout << “Float caught: ” << f << endl ;
        }
        catch( double d )
        {
            cout << “Double caught: ” << d << endl ;
        }
        catch( char *s )
        {
            cout << “Char* caught: ” << s << endl ;
        }
        catch( ... ) {
            cout << “Other exception caught” << endl ;
        }
}// for loop

    return( 0 ) ;
}

void rethrowFunc( int i )
{
    try {
        throwFunc( i ) ;
        cout << “rethrowFunc()” << endl ;
    }
    catch( int v ) {
        throw ;
    }
    catch( float f ) {
        throw ;
    }
    catch( double d ) {
        throw ;
    }
    catch( char *s ) {
        throw ;
    }
    catch( ... ) {
        throw ;
    }
}

 void throwFunc( int i )
{
    int x = 5, y = 0, r = 0 ;

    switch( i )
    {
    case 0:
        throw( int(5) ) ;
        break ;
    case 1:
        throw( float(10.5) ) ;
        break ;
    case 2:  throw( double(21.0) ) ;
        break ;
    case 3:
        throw( “an exceptional string” ) ;
        break ;
    default:
        r = x / y ;
        break ;
    }
}
2.  Save the file as CATCH.CPP. Next, exit out of the editor, return to the command line, and run the compiler and linker, naming CATCH.CPP as the input file.
3.  Run the CATCH program. The following should be displayed:
Int caught: 5
Float caught: 10.5
Double caught: 21
Char* caught: an exceptional string
Other exception caught
Other exception caught

The source code will be examined in detail in the following section.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.