PreviousNext

Defining Epilogue Actions for a Block

A FINALLY mechanism is provided so that multithreaded programs can restore invariants as certain scopes are unwound; for example, restoring shared data to a correct state and releasing locks. This is often the ideal way to define, in one place, the cleanup activities for normal or abnormal exit from a block that has changed some invariant.

Following is an example of specifying an invariant action whether or not there is an error:

lock_tape_drive (t);

TRY

TRY

read_tape ();

CATCH (parity_error)

printf ("Oops, parity error, program terminating\n");

printf ("Try cleaning the heads!\n");

RERAISE;

ENDTRY

/* Control gets here only if no exception is raised */

/* ... Now we can use the data off the tape */

FINALLY

/* Control gets here normally, or if any exception is */

raised unlock_tape_drive (t);

ENDTRY