3 * $Id: exc.h,v 1.1 1998/06/17 23:44:42 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 General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (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 General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with mLib; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.1 1998/06/17 23:44:42 mdw
46 /*----- Quick documentation -----------------------------------------------*
48 * This header file provides some exception handling facilities in C
49 * programs. It modifies the syntax of the language slightly, using the
52 * The `throw' expression returns no value. It has the syntax:
54 * THROW ( expr , expr )
56 * The first expression must have type compatible with unsigned integer; it
57 * identifies an `exception type'. The second must have type compatible
58 * with pointer to void; it contains the `exception data'. Control is
59 * passed to the current exception handler.
61 * The `RETHROW' expression, valid only within an exception handler, causes
62 * the current exception to be thrown again.
64 * A `try' statement has the syntax:
66 * TRY stat CATCH stat END_TRY;
68 * The first statement is called the `test'; the second is the `handler'.
69 * During execution of the test, the handler is added to a stack of
70 * active exception handlers; the topmost handler on this stack is called
71 * the `current' handler. When execution of the test completes, the
72 * corresponding handler is removed from the stack.
74 * The test statement may complete in one of these ways:
76 * * Normal completion -- control reaches the end of the statement
79 * * Throwing an exception -- an exception is thrown when the handler is
80 * the current exception handler.
82 * * By executing a `break' statement.
84 * * By executing the expression `EXIT_TRY' and transferring control to
85 * a point outside the entire `try' statement (e.g., executing a `goto'
86 * or `return' statement).
88 * Any other attempt to leave the test causes undefined behaviour.
90 * If an exception is thrown while the handler is the current exception
91 * handler, it is given control. The variables `exc_type' and `exc_val'
92 * denote the exception type and value respectively -- they are passed
93 * unchanged from the `throw' expression which caused the exception.
94 * A handler is deactivated before it is invoked; if it causes an
95 * exception to be thrown (and does not contain a nested `try' statement)
96 * control will be passed to an earlier active handler.
98 * Control is passed to handlers using the `longjmp' function.
103 * ... something dangerous ...
104 * } CATCH switch (exc_type) {
105 * case EXC_INTERESTING:
106 * ... handle exception ...
109 * ... do tidying up ...
114 /*----- Exception type allocation -----------------------------------------*
116 * Nobody allocates exception types, so we'll just have to try to get along
117 * without too many collisions. An exception type is an unsigned long,
118 * which gives us four bytes. The top two bytes identify the library which
119 * `owns' the exception, with special values zero meaning `defined as part
120 * of the system' and 0xFFFF providing a shared space of types which can
121 * be used by anyone as long as they don't get seen by anyone else.
123 * The lower byte pair encodes a type number, and a value which defines
124 * the type of the value field (see below).
127 /* --- Type type of an exception --- */
129 typedef unsigned long exc_extype;
131 /* --- Build a byte pair from two characters --- *
133 * Note the icky casting to handle signed chars.
136 #define EXC_PAIR(x, y) (((unsigned long)(unsigned char)(x) << 8) | \
137 (unsigned long)(unsigned char)(y))
139 /* --- Allocate an exception number --- */
141 #define EXC_ALLOC(owner, type) (((unsigned long)(owner) << 16) | \
142 (unsigned long)(type))
144 /* --- Special owner codes --- */
146 #define EXC_GLOBAL 0u /* The global space defined here */
147 #define EXC_SHARED 0xFFFFu /* The shared space for everyone */
149 /*----- Exception values --------------------------------------------------*
151 * Exception values can have several different types. This is a mess, and
152 * C doesn't handle it too well, but we can try. I'll encode the value type
153 * as part of the exception type, in the top bits of the bottom byte. Messy?
157 /* --- Encoding a value type in an extype --- */
159 #define EXC_TYPECODE(t, w) (((w) & ~0xC0u) | ((t) & 0xC0u))
161 /* --- The various value types --- */
163 #define EXC_NOVAL 0x00u /* No interesting value */
164 #define EXC_INTVAL 0x40u /* Integer value */
165 #define EXC_PTRVAL 0x80u /* Arbitrary pointer value */
166 #define EXC_STRVAL 0xC0u /* Pointer to character string */
168 /* --- Allocating exceptions with appropriate types --- */
170 #define EXC_ALLOCN(o, t) EXC_TYPECODE(EXC_NOVAL, EXC_ALLOC(o, t))
171 #define EXC_ALLOCI(o, t) EXC_TYPECODE(EXC_INTVAL, EXC_ALLOC(o, t))
172 #define EXC_ALLOCP(o, t) EXC_TYPECODE(EXC_PTRVAL, EXC_ALLOC(o, t))
173 #define EXC_ALLOCS(o, t) EXC_TYPECODE(EXC_STRVAL, EXC_ALLOC(o, t))
175 /* --- A union representing the type --- */
177 typedef union exc_exval {
183 /*----- Predefined exceptions ---------------------------------------------*/
185 /* --- @EXC_NOMEM@ --- *
189 * Meaning: An attempt to allocate memory failed.
192 #define EXC_NOMEM EXC_ALLOCN(EXC_GLOBAL, 0u)
194 /* --- @EXC_ERRNO@ --- *
196 * Value: @int errno@ = the error raised
198 * Meaning: Some kind of OS error occurred.
201 #define EXC_ERRNO EXC_ALLOCI(EXC_GLOBAL, 1u)
203 /* --- @EXC_OSERROR@ --- *
205 * Value: @os_error *e@ = pointer to error block
207 * Meaning: For RISC OS programmers only: alternative way of propagating
211 #define EXC_OSERROR EXC_ALLOCP(EXC_GLOBAL, 1u)
213 /* --- @EXC_SIGNAL@ --- *
215 * Value: @int sig@ = signal number
217 * Meaning: Report the raising of a signal.
220 #define EXC_SIGNAL EXC_ALLOCI(EXC_GLOBAL, 2u)
222 /* --- @EXC_FAIL@ --- *
224 * Value: @const char *p@ = pointer to expanatory string
226 * Meaning: Miscellaneous error.
229 #define EXC_FAIL EXC_ALLOCS(EXC_GLOBAL, 0xFFu)
231 /*----- An exception handler block ----------------------------------------*/
233 /* --- Try to think of this as being opaque --- */
235 typedef struct __exc_hnd {
236 struct __exc_hnd *next; /* Pointer to next record down */
237 exc_extype type; /* Type of this exception */
238 exc_exval val; /* Value of this exception */
239 jmp_buf buf; /* Jump buffer when exceptions hit */
242 /*----- Global variables --------------------------------------------------*/
244 extern __exc_hnd *__exc_list; /* List of active handlers */
246 /*----- Macros ------------------------------------------------------------*/
248 /* --- References to current exception type and value --- */
250 #define exc_type (__exc_ec.type)
251 #define exc_val (__exc_ec.val)
252 #define exc_i (__exc_ec.val.i)
253 #define exc_p (__exc_ec.val.p)
254 #define exc_s (__exc_ec.val.s)
256 /* --- How it actually works --- *
258 * A `try' block is contained within a block which provides an exception
259 * handler buffer in automatic storage. This block is a loop, to allow
260 * `break' to escape from it. It adds the handler buffer to the top of a
261 * list, and does a `setjmp' to allow a return here following an exception.
262 * The `setjmp' returns zero for the `try' section, and nonzero if there's
263 * an exception to `catch'. It looks a little like this:
268 * if (!setjmp(h.buf)) {
269 * do <try code> while (0);
270 * remove_handler(&h);
275 * Everything else is ugly hacking to make things work.
278 /* --- Trying things which may cause exceptions --- */
281 volatile __exc_hnd __exc_ec; \
282 __exc_ec.next = __exc_list; \
283 __exc_list = (__exc_hnd *)&__exc_ec; \
284 if (!setjmp(*(jmp_buf *)&__exc_ec.buf /* very nasty! */ )) { do
286 #define EXIT_TRY do __exc_list = __exc_ec.next; while (0)
287 #define CATCH while (0); EXIT_TRY; } else
289 #define END_TRY } while (0)
291 /* --- Raising exceptions --- */
293 #define THROW __exc_throw
294 #define RETHROW __exc_rethrow(__exc_ec.type, __exc_ec.val)
296 /*----- Functions ---------------------------------------------------------*/
298 /* --- @exc_uncaught@ --- *
300 * Arguments: @void (*proc)(exc_extype type, exc_exval val) = new handler
302 * Returns: Pointer to the old handler value.
304 * Use: Sets the handler for uncaught exceptions.
307 typedef void (*exc__uncaught)(exc_extype /*type*/, exc_exval /*val*/);
308 extern exc__uncaught exc_uncaught(exc__uncaught /*proc*/);
310 /* --- @__exc_throw@ --- *
312 * Arguments: @exc_extype type@ = type of exception to throw
316 * Use: NOT FOR USER CONSUMPTION. Reads an appropriate exception
317 * value and throws an exception.
320 extern void __exc_throw(exc_extype /*type*/, ...);
322 /* --- @__exc_rethrow@ --- *
324 * Arguments: @exc_extype type@ = type of exception to throw
325 * @exc_exval val@ = value of exception to throw
329 * Use: NOT FOR USER CONSUMPTION. Does the donkey-work of raising
333 extern void __exc_rethrow(exc_extype /*type*/, exc_exval /*val*/);
335 /*----- That's all, folks -------------------------------------------------*/