HP C/aC++ for PA-RISC Software Exceptions Thrown by the Standard C++ Library, Typeinfo Class

Models: C/aC++ for PA-RISC Software

1 230
Download 230 pages 50.97 Kb
Page 153
Image 153

Exceptions Thrown by the Standard C++ Library

The following exceptions are thrown by the Standard C++ Library:

operator new () and operator new [ ] throw a bad_alloc exception when they cannot obtain a block of storage.

A dynamic_cast expression throws a bad_cast exception when a cast to a reference type fails.

Operator typeid throws a bad_type exception when a pointer to a typeid expression is zero.

A bad_exception exception can be thrown when the unexpected handler function is invoked by unexpected().

NOTE: If no catch clauses are available to catch these exceptions, the default action is program termination with a call to abort(). (Using the +noeh option does not disable the exceptions thrown by these library functions.)

type_info Class

type_info is a class in the standard header file <typeinfo>. A reference to an instance of this class is returned by the typeid operation.

Implementations may differ in the exact details of this class, but in all cases it is a polymorphic type (has virtual functions) that allows comparisons and a way to access the name of the type.

Usage:

This class is useful for diagnostic information and for implementing services on objects where it is necessary to know the exact type of the object.

Example:

#include <iostream.h>

#include <typeinfo>

class Base

{

 

virtual

void f();

// Must have a virtual

//function to be a

//polymorphic type

//additional class details omitted

};

class Derived : public Base { // class details omitted

};

void Base::f()

{

//Define function from Base.

}

int main ()

{

Base *p;

// code which does either

 

 

//

p

=

new

Base; or

 

 

//

p

=

new

Derived;

 

 

if (typeid(*p) == typeid(Base))

//

Standard requires

 

 

 

 

 

//

comparison as part

Exceptions Thrown by the Standard C++ Library 153

Page 153
Image 153
HP C/aC++ for PA-RISC Software manual Exceptions Thrown by the Standard C++ Library, Typeinfo Class