Jump to 
content
HP.com Home Products and Services Support and Drivers Solutions How to Buy
»  Contact HP

 

HP C++

HP C++
Class Library Reference Manual


Previous Contents Index


Chapter 7
Objection Package

The Objection package provides a way to implement simple error handling. You can use this package to catch run-time errors encountered in using classes, and to change or restore actions associated with such errors.


Global Declaration

This typedef is used by, but is not a member of, the Objection class.

Header

#include <objection.hxx>

Alternative Header

#include <Objection.h>


Declaration


typedef int Objection_action(const char*); 


Type

Objection_action

Is the type of an action routine that can be called by the function Objection::raise .

Objection class

Provides the capability to handle and report errors.

Header

#include <objection.hxx>

Alternative Header

#include <Objection.h>


Declaration


class Objection 
{ 
 
public: 
                      Objection(); 
                      Objection(Objection_action *); 
    int               raise(const char * = ""); 
    Objection_action  *appoint(Objection_action *); 
    Objection_action  *appoint(); 
    Objection_action  *ignore(); 
}; 


Description

This class provides ways to handle objections. An objection is a potential error condition that your program can encounter. The user appoints an error-handling function. An Objection object's raise() function invokes the appointed function by passing it a character string that contains an error message. At any point in your program, you can appoint a new error-handling function, reappoint the original function, or specify that an objection be ignored.

Constructors

Objection()

Constructs an Objection object with no default action (error handler).

Objection(Objection_action *new_action)

Constructs an Objection object with a pointer to the default error handler. The handler is a function that takes one parameter of type const char *msg and returns an int . See the raise() member function for more information.

Member Functions

Objection_action *appoint()

Specifies that the handler for the objection is the default error handler (if one exists) and returns the previous action associated with the specified objection. Specifies that the objection not be ignored.

Objection_action *appoint(Objection_action *new_action)

Specifies a new handler for the objection and returns the previous action associated with the specified objection. Specifies that the objection not be ignored.

Objection_action *ignore()

Specifies that the objection be ignored (no error handler is invoked if the objection is raised). This function returns the previous action associated with the specified objection.

int raise(const char *msg = "")

Raises a specified objection, passing a string (error message) to an error handler (if one exists). If no handler exists, or if the handler returns a 0, the default handler is called. The raise function returns the value returned by the last handler it called.

If no default handler exists, then the function returns 0. A 0 is also returned if the objection is ignored. Generally, the return of a nonzero value means that the error handling succeeded, and the return of a 0 value means the error handling failed.

The following example changes the default error handler for the
stack(int)::overflow_error objection:


#include <stdlib.h> 
#include <vector.hxx> 
#include <objection.hxx> 
 
vectordeclare(int) 
stackdeclare(int) 
 
vectorimplement(int) 
stackimplement(int) 
 
stack(int) s(10); 
 
int error(const char *errmsg) 
{ 
    cerr << "ERROR TRAPPED: " << errmsg << " -- ABORTING\n"; 
    cerr.flush(); 
    abort(); 
    return 0; 
} 
 
void main() 
{ 
    Objection_action *save_action; 
    save_action = stack(int)::overflow_error.appoint(error); 
    for(int i=0; i<100; i++)  //push too many things onto stack 
        s.push(i); 
    stack(int)::overflow_error.appoint(save_action); 
} 

When this example executes, the following message prints out:


ERROR TRAPPED: Stack underflow -- ABORTING 
%SYSTEM-F-OPCCUS, opcode reserved to customer fault at PC=00010BE5, PSL=03C00000 
%TRACE-F-TRACEBACK, symbolic stack dump follows 
module name     routine name                     line       rel PC    abs PC 
 
                                                           0000012D  00010BE5 
                                                           0000000E  00009346 
OBJECTION_EXAMP error                            5984      00000045  00003D29 
CXXL_OBJECTION  Objection::raise                  779      00000026  00008F5A 
OBJECTION_EXAMP main                             5993      0000005B  00003D87 
                                                           00000072  0002DB5E 

Note

The message printed on your system differs somewhat from that shown here.


Previous Next Contents Index

Privacy statement Using this site means you accept its terms
© 2005 Hewlett-Packard Development Company, L.P.