CIS-255 Home http://www.c-jump.com/bcc/c255c/c255syllabus.htm

C++ Exceptions


  1. Exceptions
  2. Error detection and handling considerations
  3. Exception mechanism
  4. What is an exception ?
  5. Example of throwing exception
  6. Throwing exceptions with info
  7. Catching exceptions
  8. throw syntax and semantics
  9. try-catch syntax and rules

Exceptions


Error detection and handling considerations


  • A function which detects an error might be able to deal with it, or might not.

  • If not, it should notify someone who can. Obvious candidates are callers of the function.

  • How do we notify callers of an error ?

  • If A calls B calls C calls D and only A can deal with the error detected by D, can we avoid involving B and C in the communication ?

  • What form should the communication take ?

function calls

Exception mechanism


  • A function which detects an error can throw an exception.

  • A function which can deal with errors can catch an exception.

  • When an exception is thrown, control "travels" up the call stack, "looking" for a function that can catch it.

  • If none is found, some default behavior is invoked (typically termination).

Exception mechanism: throw and catch

What is an exception ?


Example of throwing exception



// account.h
// A banking account:
class Account { /*...*/ };

// An exception class:
struct AcctError {};


// main.cpp
#include <iostream>
void main() {
    Account acct( 1000.00 );
    try {
        acct.withdraw( 2000.00 );
    }
    catch ( AcctError& ex ) {
        std::cout << "Not enough funds";
    }
}
Example of throwing exception

// account.cpp
void Account::withdraw( double amount )
{
    if ( amount > m_available_balance ) {
        // Not enough balance,
        // Unnamed temporary object:
        throw AcctError();
    }
    // Balance is ok, proceed...
}

Throwing exceptions with info



    struct AcctError
    {
        std::string problem;
        AcctError( std::string str )
        {
            problem = str;
        }
    };

    void Account::withdraw( double amount )
    {
        if ( amount > m_available_balance )
        {
            throw AcctError( "Insufficient funds." );
        }
        //...
    }

Catching exceptions



    #include <iostream>
    #include "account.h"
    int main()
    {
        Account acct;
        acct.deposit( 100.00 );

        try {
            acct.withdraw( 1000000.00 );
        }

        catch ( AcctError& e ) {
            std::cout << "Error: " << e.problem << endl;
            return 1;
        }

        return 0;
    }

throw syntax and semantics


try-catch syntax and rules


  • A try block must use braces {} and has its own scope.

  • A try block must be followed by one or more catch blocks.

  • A catch block must use braces {} and is a scope.

  • Each catch block must have a declaration list

  • Declaration lists declare one type, with an optional identifier.

  • The catch block can treat the identifier like a local variable.

  • catch block identifiers shadow other local variables.

  • It is possible to catch *any* exception:

    
    void f()
    {
        try
        {
            acct.withdraw( 2000000 );
        }
        catch ( ... )
        {
            // free dynamic memory:
            delete[] my_temp_array;
            throw;
        }
    }