[Return to Bookshelf] [Contents] [Previous Section] [Next Section] [Index] [Help]


5.2.5 Catching All Exceptions

The exception scope can express interest in all exceptions using the CATCH_ALL macro. No CATCH macros can follow the CATCH_ALL macro within an exception scope.

Any exception that is caught using a CATCH_ALL macro should be reraised. It is inappropriate to absorb exceptions that your code is not explicitly aware of. Because you cannot necessarily predict all possible exceptions that your code might encounter, you cannot assume that your code can recover in every possible situation. Therefore, your CATCH_ALL clause should reraise all exceptions to allow an outer scope to catch this specific exception and perform the appropriate recovery.

Following is an example of the CATCH_ALL macro.

int *local_mem;
local_mem = malloc (sizeof (int));
TRY {
   operation(local_mem);      /* May raise an exception */
   free (local_mem);
}
CATCH (an_error) {
   printf ("Oops; caught one!\n");
   free (local_mem);
   RERAISE;
}
CATCH_ALL {
   free (local_mem);
   RERAISE;
}
ENDTRY