Exception Handling in ANSI C

Harald Winroth

Technical report, Department of Numerical Analysis and Computing Science, Royal Institute of Technology, S-100 44 Stockholm, Sweden, Apr. 1993. (ISRN KTH NA/P--93/15--SE)

Shortened version in C Users Journal, Oct. 1993, vol. 11, no. 10, pp. 33-46.

Abstract

An exception is an unusual condition which the main body of code has not been designed to handle. Exception handling is a technique that allows control to be transferred to an alternative block of code when an exception has been detected.

We present a set of macros which supports exception handling in ANSI C. Source codenadang theseCVAP/ macros needs no special pre-processing; the usual C pre-processor pass will suffice.

Exceptions can be defined, thrown and caught with a uniform block-oriented syntax. Top-level handlers can be installed for exceptions that would otherwise not be caught. The package also includes a callback facility which can restore user-defined data structures when exceptions are thrown.

Two application examples are presented: an error message handler and a scheme for translating signals into exceptions.

Keywords: C, exception, unwind, signal


Harald Winroth <harald@bion.kth.se>