HPlogo HP C++ Programmer's Guide: HP 9000 Series Workstations and Servers > Chapter 1 Overview of HP C++

Exception Handling

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

HP C++ version 3.0 added a mechanism to respond to error conditions in a controlled way. This mechanism is called exception handling. Exception conditions are error situations that occur while your program is running.

For more information about exception handling, see the The C++ Programming Language and the HP C++ Release Notes.

You Must Use the +eh Option

To use exception handling, you must use the +eh option. If your program consists of multiple source files, you must compile all the files in the program with the +eh option. If some files were compiled with +eh and some without, when you link with the CC command, c++patch will give an error and the files will not link.

The throw, catch, and try Statements

To signal an error condition, you "raise an exception" with the throw statement. To respond to the error condition, you "handle the exception" with the catch statement. The throw statement must appear either within a try block, which is defined by the keyword try, or in functions called from the try block. The catch statement must appear immediately after the try block.

Examples

For example, the following declares a class Stack, which is an integer stack of a maximum of 5 elements. The class Stack declares two public nested classes Overflow and Underflow which will be used for handling those error conditions. When the stack overflows or underflows, the appropriate exceptions are thrown:

#include <iostream.h> 
const STACKMAX=5;      // Maximum size of the stack.
               
class Stack           
{               
public:        
               
     class Overflow    // An exception class.
     { public:                
             int overflowval;               
             Overflow(int i) : overflowval(i) {}
     };

     class Underflow           // An exception class.
     { public: 
             Underflow () {}
     }

     Stack(){top=-1;}
     void push(int item) 
         {if (top<(STACKMAX-1)) thestack[++top]=item;
          else throw Overflow(item);}
     int pop() 
         {if (top>-1) return thestack[top--];
          else throw Underflow();}
private:
     int thestack[STACKMAX];
     int top;
};

The following main program declares a stack and exception handlers for the overflow and underflow stack conditions. The program forces the stack to overflow causing the exception handler to be invoked.

#include <iostream.h>
void main()
{ 
    Stack mystack;
    int i=5, j=25;

// Here is the try block where 
// exception handlers are available.

    try 
    {   mystack.push(i);
        mystack.push(j);
        mystack.push(1);
        mystack.push(1234);
        mystack.push(999);

// Stack is now full.  Force an exception:

        mystack.push(50); // This will throw Stack::Overflow.
    }

// Here are the exception handlers.

    catch (Stack::Overflow& s)
    {   
        cout << "Stack has overflowed trying to push: " 
             << s.overflowval << endl;
    }
    catch (Stack::Underflow& s)
    {   cout << "Stack underflow has occurred." << endl; }
}

The above program displays the following message:

Stack has overflowed trying to push: 50
© Hewlett-Packard Development Company, L.P.