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 code that follows the “catch all” handler checks to see if the pointer is 0. It is possible that your compiler’s implementation of new does not throw an exception. The alternative is to check the value of the pointer; a return value of 0 indicates failure, otherwise the pointer will contain a valid memory address. In the example, if s is 0, the program throws a string to the calling function.

Let’s reexamine the calling block:

try
{
    acquireMemory( 30000 ) ;
}
catch( string s )
{
    cout << s << endl ;
}
catch( ... )
{
    cout << “Exception thrown for “ ;
    cout << “acquireMemory()” << endl ;
}

The first catch clause handles an exception of type string. This handler will process the throw string statement in function acquireMemory. If any other exception is thrown, the “catch all” handler will process the exception. The exception mechanism searches all the catch clauses, looking for the clause that can process the exception.

Moving through the source code, you come to the next try/catch section, shown in the following code:

try
{
    for( int i = 1; i >= 0; i++ )
    {
     calculate( i ) ;
     cout << “calculate(“ << i ;
     cout << “) succeeded” << endl ;
    }
}
catch( int ec )
{
    cout << “exception for calculate()” << endl ;
}

The try block surrounds a for loop. Within the for loop, the function calculate is called. Notice that the catch clause will handle an exception of type int. Now turn your attention to the function calculate to see what is happening there. The function’s source is as follows:

void calculate( int value )
{
    if( value == 0 )
    {
        errorCode = 1 ;
        throw errorCode ;
    }
}

The argument’s value is compared to 0; if the expression is true, the two statements within the if block are executed. The first statement assigns the value 1 to errorCode; this variable is defined in the global namespace. The next line is a throw statement, throwing errorCode.

Comments

An optional method for error handling is to return user-defined codes for a function call. This method was commonly accepted before the specification of the exception handling mechanism. In fact, this style is still practiced today. The following example demonstrates this style:

bool openFile( const char *name )
{
    bool successfullyOpened = false ;
    ifstream in( name ) ;
    if( in )
    {
        //...
    }
    return( successfullyOpened ) ;
}

//...
if( openFile( “sample.txt”) == true )
    //...

The return value from a function specifies the success or failure of its operation. Many third-party libraries use the int type to specify error codes. For example, a library that manipulates a database might experience different types of errors depending on the access context. As a rule, the value 0 is commonly used to signify success and non-zero values as error codes.

What happens if an exception is thrown, but not caught? The function std::terminate will be called.

It is good programming practice to include exception specifications as part of your function declarations. This permits client programmers to know the types of exceptions that your functions will throw. Remember that an empty exception specification guarantees that the function will not throw any exceptions. The lack of an exception specification tells client programmers “This function might throw any number of exceptions.”


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.