Chapter 22. Exceptions

I l @ ve RuBoard

How glorious it is ”and also how painful ”to be an exception.

”Alfred de Musset

Airplanes fly from one place to another and 99.9% of the time there's no trouble. But when there is trouble, such as a stuck wheel or an engine fire, pilots are trained to handle the emergency.

Let's examine in detail what happens during an airborne emergency such as an engine catching fire. This is an exception to normal flight. First, a fire alarm goes off in the cockpit. The alarm catches the pilots' attention, and they start going through the fire-emergency procedure. This is an extensive list of things to do in case of fire. The airline prepared this list ahead of time, and the pilots have the list memorized. The pilots do what's necessary to handle the exception: activate the fire extinguisher, shut down the engine, land very quickly, etc.

Let's translate this procedure into C++ pseudocode. When the pilots take off they are going to try to fly the plane from one point to another without problems. The C++ "code" for this is:

 try {      fly_from_point_a_to_point_b(  ); } 

The try keyword indicates that we are going to attempt an operation that may cause an exception.

But what happens when we get an exception? We need to handle it. The C++ code for this is:

 catch (fire_emergency& fire_info) {     active_extinguisher(fire_info.engine);     turn_off(fire_info.engine);     land_at_next_airport(  ); } 

The keyword catch indicates that this section of code handles an exception. In this case the exception handled is a fire_emergency . This is the type of emergency. It could be a fire in engine number 1, engine number 2, or engine number 3 ( assuming a three-engine plane). Which engine is on fire is stored in the variable fire_info .

The fire_emergency class describes what type of fire occurred. Its definition is:

 class fire_emergency {     public:         int engine;    // Which engine is on fire         // Other information about the fire }; 

We've covered everything but the actual detection of the fire. Buried within each engine is a fire sensor. The code for this sensor is:

 // Watch for fire in engine #2 void sensor_2(  ) {     while (engine_running(  )) {         if (engine_on_fire(  )) {             fire_emergency fire_info;                          fire_info.engine = 2;             throw(fire_info);         }     } } 

When this code senses a fire, it puts the information in a fire_emergency variable named fire_info and triggers an exception with the throw statement.

When the throw statement is executed, normal processing is stopped. After all, when a fire occurs, normal flying is stopped . Execution is transferred to the catch statement for the fire_emergency .

To summarize, exception handling consists of:

  • A description of a possible problem, in this case the fire_emergency class.

  • A section of code in which the exception may occur, which is enclosed in a try statement. In this case, the statement is fly_from_point_a_to_point_b( ) .

  • Something that causes an exception and triggers the emergency procedures through a throw statement.

  • Exception-handling code inside a catch block.

I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net