3 * $Id: exc.h,v 1.7 2004/04/08 01:36:11 mdw Exp $
5 * Structured exception handling in C
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
39 /*----- Quick documentation -----------------------------------------------*
41 * This header file provides some exception handling facilities in C
42 * programs. It modifies the syntax of the language slightly, using the
45 * The `throw' expression returns no value. It has the syntax:
47 * THROW ( expr , expr )
49 * The first expression must have type compatible with unsigned integer; it
50 * identifies an `exception type'. The second must have type compatible
51 * with pointer to void; it contains the `exception data'. Control is
52 * passed to the current exception handler.
54 * The `RETHROW' expression, valid only within an exception handler, causes
55 * the current exception to be thrown again.
57 * A `try' statement has the syntax:
59 * TRY stat CATCH stat END_TRY;
61 * The first statement is called the `test'; the second is the `handler'.
62 * During execution of the test, the handler is added to a stack of
63 * active exception handlers; the topmost handler on this stack is called
64 * the `current' handler. When execution of the test completes, the
65 * corresponding handler is removed from the stack.
67 * The test statement may complete in one of these ways:
69 * * Normal completion -- control reaches the end of the statement
72 * * Throwing an exception -- an exception is thrown when the handler is
73 * the current exception handler.
75 * * By executing a `break' statement.
77 * * By executing the expression `EXIT_TRY' and transferring control to
78 * a point outside the entire `try' statement (e.g., executing a `goto'
79 * or `return' statement).
81 * Any other attempt to leave the test causes undefined behaviour.
83 * If an exception is thrown while the handler is the current exception
84 * handler, it is given control. The variables `exc_type' and `exc_val'
85 * denote the exception type and value respectively -- they are passed
86 * unchanged from the `throw' expression which caused the exception.
87 * A handler is deactivated before it is invoked; if it causes an
88 * exception to be thrown (and does not contain a nested `try' statement)
89 * control will be passed to an earlier active handler.
91 * Control is passed to handlers using the `longjmp' function.
96 * ... something dangerous ...
97 * } CATCH switch (exc_type) {
98 * case EXC_INTERESTING:
99 * ... handle exception ...
102 * ... do tidying up ...
107 /*----- Exception type allocation -----------------------------------------*
109 * Nobody allocates exception types, so we'll just have to try to get along
110 * without too many collisions. An exception type is an unsigned long,
111 * which gives us four bytes. The top two bytes identify the library which
112 * `owns' the exception, with special values zero meaning `defined as part
113 * of the system' and 0xFFFF providing a shared space of types which can
114 * be used by anyone as long as they don't get seen by anyone else.
116 * The lower byte pair encodes a type number, and a value which defines
117 * the type of the value field (see below).
120 /* --- Type of an exception --- */
122 typedef unsigned long exc_extype;
124 /* --- Build a byte pair from two characters --- *
126 * Note the icky casting to handle signed chars.
129 #define EXC_PAIR(x, y) (((unsigned long)(unsigned char)(x) << 8) | \
130 (unsigned long)(unsigned char)(y))
132 /* --- Allocate an exception number --- */
134 #define EXC_ALLOC(owner, type) (((unsigned long)(owner) << 16) | \
135 (unsigned long)(type))
137 /* --- Special owner codes --- */
139 #define EXC_GLOBAL 0u /* The global space defined here */
140 #define EXC_SHARED 0xFFFFu /* The shared space for everyone */
141 #define EXC_MLIB EXC_PAIR('m', 'L') /* Space for mLib exceptions */
143 /*----- Exception values --------------------------------------------------*
145 * Exception values can have several different types. This is a mess, and
146 * C doesn't handle it too well, but we can try. I'll encode the value type
147 * as part of the exception type, in the top bits of the bottom byte. Messy?
151 /* --- Encoding a value type in an extype --- */
153 #define EXC_TYPECODE(t, w) (((w) & ~0xC0u) | ((t) & 0xC0u))
155 /* --- The various value types --- */
157 #define EXC_NOVAL 0x00u /* No interesting value */
158 #define EXC_INTVAL 0x40u /* Integer value */
159 #define EXC_PTRVAL 0x80u /* Arbitrary pointer value */
160 #define EXC_STRVAL 0xC0u /* Pointer to character string */
162 /* --- Allocating exceptions with appropriate types --- */
164 #define EXC_ALLOCN(o, t) EXC_TYPECODE(EXC_NOVAL, EXC_ALLOC(o, t))
165 #define EXC_ALLOCI(o, t) EXC_TYPECODE(EXC_INTVAL, EXC_ALLOC(o, t))
166 #define EXC_ALLOCP(o, t) EXC_TYPECODE(EXC_PTRVAL, EXC_ALLOC(o, t))
167 #define EXC_ALLOCS(o, t) EXC_TYPECODE(EXC_STRVAL, EXC_ALLOC(o, t))
169 /* --- A union representing the type --- */
171 typedef union exc_exval {
177 /*----- Predefined exceptions ---------------------------------------------*/
179 /* --- @EXC_NOMEM@ --- *
183 * Meaning: An attempt to allocate memory failed.
186 #define EXC_NOMEM EXC_ALLOCN(EXC_GLOBAL, 0u)
188 /* --- @EXC_ERRNO@ --- *
190 * Value: @int errno@ = the error raised
192 * Meaning: Some kind of OS error occurred.
195 #define EXC_ERRNO EXC_ALLOCI(EXC_GLOBAL, 1u)
197 /* --- @EXC_OSERROR@ --- *
199 * Value: @os_error *e@ = pointer to error block
201 * Meaning: For RISC OS programmers only: alternative way of propagating
205 #define EXC_OSERROR EXC_ALLOCP(EXC_GLOBAL, 1u)
207 /* --- @EXC_SIGNAL@ --- *
209 * Value: @int sig@ = signal number
211 * Meaning: Report the raising of a signal.
214 #define EXC_SIGNAL EXC_ALLOCI(EXC_GLOBAL, 2u)
216 /* --- @EXC_FAIL@ --- *
218 * Value: @const char *p@ = pointer to expanatory string
220 * Meaning: Miscellaneous error.
223 #define EXC_FAIL EXC_ALLOCS(EXC_GLOBAL, 0xFFu)
225 /*----- An exception handler block ----------------------------------------*/
227 /* --- Try to think of this as being opaque --- */
229 typedef struct __exc_hnd {
230 struct __exc_hnd *next; /* Pointer to next record down */
231 exc_extype type; /* Type of this exception */
232 exc_exval val; /* Value of this exception */
233 jmp_buf buf; /* Jump buffer when exceptions hit */
236 /*----- Global variables --------------------------------------------------*/
238 extern __exc_hnd *__exc_list; /* List of active handlers */
240 /*----- Macros ------------------------------------------------------------*/
242 /* --- References to current exception type and value --- */
244 #define exc_type (__exc_ec.type)
245 #define exc_val (__exc_ec.val)
246 #define exc_i (__exc_ec.val.i)
247 #define exc_p (__exc_ec.val.p)
248 #define exc_s (__exc_ec.val.s)
250 /* --- How it actually works --- *
252 * A `try' block is contained within a block which provides an exception
253 * handler buffer in automatic storage. This block is a loop, to allow
254 * `break' to escape from it. It adds the handler buffer to the top of a
255 * list, and does a `setjmp' to allow a return here following an exception.
256 * The `setjmp' returns zero for the `try' section, and nonzero if there's
257 * an exception to `catch'. It looks a little like this:
262 * if (!setjmp(h.buf)) {
263 * do <try code> while (0);
264 * remove_handler(&h);
269 * Everything else is ugly hacking to make things work.
272 /* --- Trying things which may cause exceptions --- */
275 volatile __exc_hnd __exc_ec; \
276 __exc_ec.next = __exc_list; \
277 __exc_list = (__exc_hnd *)&__exc_ec; \
278 if (!setjmp(*(jmp_buf *)&__exc_ec.buf /* very nasty! */ )) { do
280 #define EXIT_TRY do __exc_list = __exc_ec.next; while (0)
281 #define CATCH while (0); EXIT_TRY; } else
283 #define END_TRY } while (0)
285 /* --- Raising exceptions --- */
287 #define THROW __exc_throw
288 #define RETHROW __exc_rethrow(__exc_ec.type, __exc_ec.val)
290 /*----- Functions ---------------------------------------------------------*/
292 /* --- @exc_uncaught@ --- *
294 * Arguments: @void (*proc)(exc_extype type, exc_exval val) = new handler
296 * Returns: Pointer to the old handler value.
298 * Use: Sets the handler for uncaught exceptions.
301 typedef void (*exc__uncaught)(exc_extype /*type*/, exc_exval /*val*/);
302 extern exc__uncaught exc_uncaught(exc__uncaught /*proc*/);
304 /* --- @__exc_throw@ --- *
306 * Arguments: @exc_extype type@ = type of exception to throw
310 * Use: NOT FOR USER CONSUMPTION. Reads an appropriate exception
311 * value and throws an exception.
314 extern void __exc_throw(exc_extype /*type*/, ...);
316 /* --- @__exc_rethrow@ --- *
318 * Arguments: @exc_extype type@ = type of exception to throw
319 * @exc_exval val@ = value of exception to throw
323 * Use: NOT FOR USER CONSUMPTION. Does the donkey-work of raising
327 extern void __exc_rethrow(exc_extype /*type*/, exc_exval /*val*/);
329 /*----- That's all, folks -------------------------------------------------*/