HP Integrity NonStop H-Series User Manual

Page 225

Advertising
background image

To throw a base exception you would use the following code:

throw exception;

This is generally not very useful, since whatever catches this exception will have no idea what
kind of error has occurred. Instead of a base exception, you will usually throw a derived class
such as logic_error or one of its derivations (such as out_of_range as shown in the example
above). Better still, you can extend the hierarchy by deriving your own classes. This allows you
to provide error reporting specific to your particular problem. For instance:

class bad_packet_error : public runtime_error
{
public:
bad_packet_error(const string& what);
};

if (bad_packet())
throw bad_packet_error("Packet size incorrect");

This demonstrates how the Standard C++ exception classes provide you with a basic error
model. From this foundation you can build the right error detection and reporting methods
required for your particular application.

Advertising
This manual is related to the following products: