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


Steps

1.  Create a new directory named EXCEPTH under your base source directory and make EXCEPTH the current directory.
2.  Start up your source code editor and type in the following source code, exactly as shown.
// filename: excepth.cpp - program to demonstrate
// basic exception handling in C++
#include <iostream>
using namespace std ;
const int DB_OK = 0 ;
const int DB_NOT_INIT = -1 ;
const int DB_ACCESS_ERROR = -2 ;

int dbInit( ) ;
int dbOpen( const char *fname ) ;
int dbGetData( int field, char *data ) ;
int dbClose( int handle ) ;

void dbInitEH( ) ;
int  dbOpenEH( const char *fname ) ;
void dbGetDataEH( int field, char *data ) ;
void dbCloseEH( int handle ) ;

int main( )
{
    // manipulating a database,
    // traditional error handling.
    if( dbInit( ) == DB_OK )
    {
        int theHandle = dbOpen( “test.dat” ) ;

        if( theHandle > 0 )
        {
            char data[ 30 ] ;

            if( dbGetData( 1, data ) == DB_OK )
                cout << “Data is: ” << data << endl ;
            else
                cout << “Error getting data” << endl ;

            if( dbClose( theHandle ) != DB_OK )
                cout << “Error closing dB” << endl ;
            }
            else
                cout << “Error opening DB” << endl ;
        }
        else
                cout << “Error initializing DB” << endl ;

        // manipulating a database,
        // the exception handling way
        try {
            char data[ 30 ] = “” ;

            dbInitEH( ) ;
            int theHandle = dbOpenEH( “test.dat” ) ;
            dbGetDataEH( 1, data ) ;
            cout << “Data is: ” << data << endl ;
            dbCloseEH( theHandle ) ;
        }
        catch( int v ) {
            cout << “Error with DB: ” << v << endl ;
        }
            return( 0 ) ;
        }
        int dbInit( )
        {
            return( DB_OK ) ;
        }
        int dbOpen( const char *fname )
        {
            return( 2 ) ;
        }

        int dbGetData( int field, char *data )
        {
            int i = 0 ;
            for(  ; i < 10; i++ )
                data[ i ] = i + 48 ;
            data[ i ] = 0x00 ;

            return( DB_OK ) ;
        }

        int dbClose( int handle )
        {
            return( DB_OK ) ;
        }

        void dbInitEH( )
        {
        }

        int dbOpenEH( const char *fname )
        {
            return( 2 ) ;
        }

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

        void dbCloseEH( int handle )
        {
        }
3.  Save the file, naming it EXCEPTH.CPP. Then exit the editor and return to the command line.
4.  At the command line, type the command required to compile and link the program. For example, if you are using Microsoft Visual C++, you type cl first.cpp. If you are using DJGPP, type gcc excepth.cpp. The compiler and linker will run and (if you typed all source text correctly) return to the command line without any error messages.
5.  At the command prompt, run the final executable (called excepth). If you are on a UNIX system and you do not see a file named excepth, look for a file named a.out; if you find this filename, execute it. You should see the following output on your screen:
Data is: 0123456789
Error with DB: -2

The next section, “How It Works,” will discuss how this program operates.

How It Works

After the two source comment lines, you see the include directive for iostream. On the next line is the using directive, effectively making all names visible in namespace std into the current scope.

The next three lines of code declare and define three constant values.

const int DB_OK = 0 ;
const int DB_NOT_INIT = -1 ;
const int DB_ACCESS_ERROR = -2 ;

These values are used as status codes for the “dummy” database functions. These values are representative of some well-known Indexed Sequential Access Methods (ISAM) C libraries. Not that the values themselves match the libraries’ codes, but rather the logic of using negative integer values to represent error codes.

The next four lines

int dbInit( ) ;
int dbOpen( const char *fname ) ;
int dbGetData( int field, char *data ) ;
int dbClose( int handle ) ;

are function declarations representing traditional C-style ISAM functions. This program does not actually access any data files, but simulates the logic of doing so. The functions are described in more detail later in this section.

The next four source lines

void dbInitEH( ) ;
int  dbOpenEH( const char *fname ) ;
void dbGetDataEH( int field, char *data ) ;
void dbCloseEH( int handle ) ;

declare four functions oriented around exception handling. The function names match the previous function names with the string EH appended to the names. The EH designates that these functions utilize exception handling. Also, notice that all but one of these functions return void, whereas the previous four functions return an int.

Now, let’s take a step back and examine the following section of code. Notice all the if-else statements; this code is almost hard to follow. You really have to pay attention to what is happening here.

if( dbInit( ) == DB_OK )
{
    int theHandle = dbOpen( “test.dat” ) ;

    if( theHandle > DB_OK )
    {
        char data[ 30 ] ;

        if( dbGetData( 1, data ) == DB_OK )
            cout << “Data is: ” << data << endl ;
        else
            cout << “Error getting data” << endl ;

        if( dbClose( theHandle ) != DB_OK )
            cout << “Error closing DB” << endl ;
    }
    else
        cout << “Error opening DB” << endl ;
}
else
     cout << “Error initializing DB” << endl ;

The first line in this section of code calls the dbInit() function to initialize the database. The return value from that function specifies whether the initialization is successful. Otherwise, the associated else reports an error.


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.