Frequently, the only reason a block of code needs to catch exceptions is to perform cleanup actions, such as releasing resources. In many cases, the same operations are performed whether the block exits normally or with an exception; under many exception models, this requires duplicating code (both within a CATCH_ALL type construct, and following the exception scope in case an exception does not occur).
The FINALLY macro catches an exception and then reraises the exception for outer scopes to handle. The actions defined by a FINALLY clause are also performed when the scope exits normally without an exception, so that they do not need to be duplicated.
Do not combine the FINALLY clause with CATCH or CATCH_ALL. Doing so results in unpredictable behavior.
Following is an example of the FINALLY macro:
int *local_mem; local_mem = malloc (sizeof (int)); TRY { operation(local_mem); /* May raise an exception */ } FINALLY { free (local_mem); } ENDTRY