Add a slew of manual pages.
[mLib] / man / exc.3
CommitLineData
b6b9d458 1.\" -*-nroff-*-
2.de VS
3.sp 1
4.in +5n
5.ft B
6.nf
7..
8.de VE
9.ft R
10.in -5n
11.sp 1
12.fi
13..
14.TH exc 3mLib "20 June 1999" mLib
15.SH NAME
16exc \- exception handling for C programs
17.SH SYNOPSIS
18.B "#include <mLib/exc.h>
19.sp 1
20.B TRY
21.I statement
22.B CATCH
23.I statement
24.B END_TRY;
25.br
26.B EXIT_TRY;
27.sp 1
28.BI "THROW(exc_extype " type
29.RB [ ,
30.IR data ]\fB);
31.br
32.B RETHROW;
33.sp 1
34.nf
35.B "typedef void (*exc__uncaught)(exc_extype, exc_exval);"
36.BI "exc__uncaught exc_uncaught(exc__uncaught " proc );
37
38.BI "exc_extype EXC_PAIR(unsigned char " x ", unsigned char " y );
39.BI "exc_extype EXC_ALLOC(exc_extype " owner ", exc_extype " type );
40.BI "exc_extype EXC_ALLOCN(exc_extype " owner ", exc_extype " type );
41.BI "exc_extype EXC_ALLOCI(exc_extype " owner ", exc_extype " type );
42.BI "exc_extype EXC_ALLOCP(exc_extype " owner ", exc_extype " type );
43.BI "exc_extype EXC_ALLOCS(exc_extype " owner ", exc_extype " type );
44.fi
45.SH DESCRIPTION
46The header file
47.B <mLib/exc.h>
48introduces some new syntax and definitions to support exception handling
49in C. The marriage is not particularly successful, although it works
50well enough in practice.
51.PP
52The syntax introduced consists of new
53.B TRY
54and
55.B EXIT_TRY
56statements and a pair of new expression types
57.B THROW
58and
59.BR RETHROW .
60It's unfortunately important to remember that the syntax is provided
61using macro expansion and standard C facilities; some of the
62restrictions of these features show through.
63.SS "The TRY statement"
64The
65.B TRY
66statement associates an exception handler with a piece of code. The
67second statement is an
68.IR "exception handler" .
69Its
70.I "dynamic scope"
71is the duration of the first statement's execution, together with the
72duration of any functions called within the dynamic scope. (Note in
73particular that an exception handler is not within its own dynamic
74scope.) A thrown exception causes the exception handler with
75dynamically innermost scope to be executed.
76.PP
77Two special variables are provided to the exception handler:
78.TP
79.B exc_type
80The
81.I type
82of the exception caught. This is value of type
83.B exc_extype
84(described below).
85.TP
86.B exc_val
87The
88.I value
89of the exception. This has a union type, with members
90.BR "int i",
91.B "void *p"
92and
93.BR "char *s" .
94Only one of the members is valid; you should be able to work out which
95from the exception type. There are abbreviations
96.BR "exc_i",
97.B exc_p
98and
99.B exc_s
100which refer to members of
101.B exc_val
102directly.
103.SS "The EXIT_TRY statement"
104It is not safe to leave the dynamic scope of an exception handler early
105(e.g., by a
106.B goto
107statement). You can force a safe exit from a dynamic scope using the
108.B EXIT_TRY
109statement from within the
110.I lexical
111scope of the
112.B TRY
113statement.
114.SS "The THROW and RETHROW statements"
115The
116.B THROW
117expression throws an exception. The first argument is the type of
118exception; the second is some data to attach to the exception. The type
119of data, integer, string or pointer, is determined from the exception
120type.
121.PP
122Control is immediately passed to the exception handler with the
123innermost enclosing dynamic scope.
124.PP
125The
126.B RETHROW
127expression is only valid within an exception handler. It rethrows the
128last exception caught by the handler.
129.PP
130Neither
131.B THROW
132nor
133.B RETHROW
134yields any value.
135.SS "Exception type allocation"
136Exception types are 32-bit values. The top 16 bits are an
137.IR "owner identifier" .
138The idea is that each library can have an owner identifier, and it can
139then allocate exceptions for its own use from the remaining space. Two
140special owner codes are defined:
141.TP
142.B "EXC_GLOBAL (0x0000)"
143The global space defined for everyone's benefit. Don't define your own
144exceptions in this space.
145.TP
146.B "EXC_SHARED (0xffff)"
147A shared space. You can use this for any exceptions which won't be seen
148by anyone else.
149.PP
150Other owner codes may be allocated by choosing two characters (probably
151letters) which best suit your application and applying the
152.B EXC_PAIR
153macro to them. For example, the owner code for
154.B mLib
155would probably be
156.BR "EXC_PAIR('m', 'L')" ,
157if
158.B mLib
159defined any exceptions other then the global ones.
160.PP
161The bottom 16 bits are the actual exception type, and the data type
162which gets passed around with the exception. The data type is
163(bizarrely) in bits 6 and 7 of the type word. The data type code is one
164of the following:
165.TP
166.B EXC_NOVAL
167There is no data associated with this exception.
168.TP
169.B EXC_INTVAL
170The data is an integer, with type
171.BR int .
172.TP
173.B EXC_PTRVAL
174The data is a pointer to some data structure, with type
175.BR "void *" .
176Note that you probably have to do an explicit cast to
177.B "void *"
178in the
179.B THROW
180expression.
181.TP
182.B EXC_STRVAL
183The data is a pointer to a string of characters, of type
184.BR "char *" .
185.PP
186If the data to be thrown is a pointer, make sure that the object pointed
187to has a long enough lifetime for it to actually get to its exception
188handler intact. In particular, don't pass pointers to automatic
189variables unless you're
190.I sure
191they were allocated outside the handler's dynamic scope.
192.PP
193Individual exceptions are allocated by the macros
194.BI EXC_ALLOC t\fR,
195where
196.I t
197is one of:
198.TP
199.B N
200The exception has no data
201.TP
202.B I
203The exception data is an integer.
204.TP
205.B P
206The exception data is a pointer.
207.TP
208.B S
209The exception data is a character string.
210.PP
211The
212.BI EXC_ALLOC t
213macros take two arguments: the owner code (usually allocated with
214.B EXC_PAIR
215as described above), and the type code. The data type is encoded into
216the exception type by the allocation macro.
217.SS "Predefined exceptions"
218The following exceptions are predefined:
219.TP
220.B EXC_NOMEM
221No data. Signals an out-of-memory condition.
222.TP
223.B EXC_ERRNO
224Integer data. Signals an operating system error. The data is the value
225of
226.B errno
227associated with the error.
228.TP
229.B EXC_OSERROR
230Pointer data. Signals a RISC\ OS error. The data is a pointer to the
231RISC\ OS error block. (Non RISC\ OS programmers don't need to worry
232about this.)
233.TP
234.B EXC_SIGNAL
235Integer data. Signals a raised operating system signal. The data is
236the signal number.
237.TP
238.B EXC_FAIL
239String data. Signals a miscellaneous failure. The data is a pointer to
240an explanatory string.
241.SH BUGS
242The call to an exception handler is acheived using
243.BR longjmp (3).
244Therefore all the caveats about
245.B longjmp
246and automatic data apply. Also, note that status such as the signal
247mask is not reset, so you might have to do that manually in order to
248recover from a signal.
249.SH AUTHOR
250Mark Wooding, <mdw@nsict.org>