3 .\" Manual for exception handling
5 .\" (c) 1999, 2001, 2005, 2009, 2024 Straylight/Edgeware
8 .\"----- Licensing notice ---------------------------------------------------
10 .\" This file is part of the mLib utilities library.
12 .\" mLib is free software: you can redistribute it and/or modify it under
13 .\" the terms of the GNU Library General Public License as published by
14 .\" the Free Software Foundation; either version 2 of the License, or (at
15 .\" your option) any later version.
17 .\" mLib is distributed in the hope that it will be useful, but WITHOUT
18 .\" ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 .\" FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 .\" License for more details.
22 .\" You should have received a copy of the GNU Library General Public
23 .\" License along with mLib. If not, write to the Free Software
24 .\" Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
27 .\"--------------------------------------------------------------------------
28 .so ../defs.man \" @@@PRE@@@
30 .\"--------------------------------------------------------------------------
31 .TH exc 3mLib "20 June 1999" "Straylight/Edgeware" "mLib utilities library"
47 .\"--------------------------------------------------------------------------
49 exc \- exception handling for C programs
51 .\"--------------------------------------------------------------------------
55 .B "#include <mLib/exc.h>"
65 .BI "THROW(exc_extype " type " " "" \fR[ "" ", "data \fR] "" );
69 .B "typedef void (*exc__uncaught)(exc_extype, exc_exval);"
70 .BI "exc__uncaught exc_uncaught(exc__uncaught " proc );
72 .BI "exc_extype EXC_PAIR(unsigned char " x ", unsigned char " y );
73 .BI "exc_extype EXC_ALLOC(exc_extype " owner ", exc_extype " type );
74 .BI "exc_extype EXC_ALLOCN(exc_extype " owner ", exc_extype " type );
75 .BI "exc_extype EXC_ALLOCI(exc_extype " owner ", exc_extype " type );
76 .BI "exc_extype EXC_ALLOCP(exc_extype " owner ", exc_extype " type );
77 .BI "exc_extype EXC_ALLOCS(exc_extype " owner ", exc_extype " type );
80 .\"--------------------------------------------------------------------------
85 introduces some new syntax and definitions to support exception handling
86 in C. The marriage is not particularly successful, although it works
87 well enough in practice.
89 The syntax introduced consists of new
93 statements and a pair of new expression types
97 It's unfortunately important to remember that the syntax is provided
98 using macro expansion and standard C facilities; some of the
99 restrictions of these features show through.
101 .SS "The TRY statement"
104 statement associates an exception handler with a piece of code. The
105 second statement is an
106 .IR "exception handler" .
109 is the duration of the first statement's execution, together with the
110 duration of any functions called within the dynamic scope. (Note in
111 particular that an exception handler is not within its own dynamic
112 scope.) A thrown exception causes the exception handler with
113 dynamically innermost scope to be executed.
115 Two special variables are provided to the exception handler:
120 of the exception caught. This is value of type
127 of the exception. This has a union type, with members
132 Only one of the members is valid; you should be able to work out which
133 from the exception type. There are abbreviations
138 which refer to members of
142 .SS "The EXIT_TRY statement"
143 It is not safe to leave the dynamic scope of an exception handler early
146 statement). You can force a safe exit from a dynamic scope using the
148 statement from within the
154 .SS "The THROW and RETHROW statements"
157 expression throws an exception. The first argument is the type of
158 exception; the second is some data to attach to the exception. The type
159 of data, integer, string or pointer, is determined from the exception
162 Control is immediately passed to the exception handler with the
163 innermost enclosing dynamic scope.
167 expression is only valid within an exception handler. It rethrows the
168 last exception caught by the handler.
176 .SS "Exception type allocation"
177 Exception types are 32-bit values. The top 16 bits are an
178 .IR "owner identifier" .
179 The idea is that each library can have an owner identifier, and it can
180 then allocate exceptions for its own use from the remaining space. Two
181 special owner codes are defined:
183 .B "EXC_GLOBAL (0x0000)"
184 The global space defined for everyone's benefit. Don't define your own
185 exceptions in this space.
187 .B "EXC_SHARED (0xffff)"
188 A shared space. You can use this for any exceptions which won't be seen
191 Other owner codes may be allocated by choosing two characters (probably
192 letters) which best suit your application and applying the
194 macro to them. For example, the owner code for
197 .BR "EXC_PAIR('m', 'L')" ,
200 defined any exceptions other then the global ones.
202 The bottom 16 bits are the actual exception type, and the data type
203 which gets passed around with the exception. The data type is
204 (bizarrely) in bits 6 and 7 of the type word. The data type code is one
208 There is no data associated with this exception.
211 The data is an integer, with type
215 The data is a pointer to some data structure, with type
217 Note that you probably have to do an explicit cast to
224 The data is a pointer to a string of characters, of type
227 If the data to be thrown is a pointer, make sure that the object pointed
228 to has a long enough lifetime for it to actually get to its exception
229 handler intact. In particular, don't pass pointers to automatic
230 variables unless you're
232 they were allocated outside the handler's dynamic scope.
234 Individual exceptions are allocated by the macros
241 The exception has no data
244 The exception data is an integer.
247 The exception data is a pointer.
250 The exception data is a character string.
254 macros take two arguments: the owner code (usually allocated with
256 as described above), and the type code. The data type is encoded into
257 the exception type by the allocation macro.
259 .SS "Predefined exceptions"
260 The following exceptions are predefined:
263 No data. Signals an out-of-memory condition.
266 Integer data. Signals an operating system error. The data is the value
269 associated with the error.
272 Pointer data. Signals a RISC\ OS error. The data is a pointer to the
273 RISC\ OS error block. (Non RISC\ OS programmers don't need to worry
277 Integer data. Signals a raised operating system signal. The data is
281 String data. Signals a miscellaneous failure. The data is a pointer to
282 an explanatory string.
284 The call to an exception handler is achieved using
286 Therefore all the caveats about
288 and automatic data apply. Also, note that status such as the signal
289 mask is not reset, so you might have to do that manually in order to
290 recover from a signal.
292 .\"--------------------------------------------------------------------------
297 .\"--------------------------------------------------------------------------
300 Mark Wooding, <mdw@distorted.org.uk>
302 .\"----- That's all, folks --------------------------------------------------