1 | \chapter{Exception Features} |
---|
2 | |
---|
3 | This chapter covers the design and user interface of the \CFA |
---|
4 | exception-handling mechanism (EHM). % or exception system. |
---|
5 | |
---|
6 | % We should cover what is an exception handling mechanism and what is an |
---|
7 | % exception before this. Probably in the introduction. Some of this could |
---|
8 | % move there. |
---|
9 | \paragraph{Raise / Handle} |
---|
10 | An exception operation has two main parts: raise and handle. |
---|
11 | These are the two parts that the user will write themselves and so |
---|
12 | might be the only two pieces of the EHM that have any syntax. |
---|
13 | These terms are sometimes also known as throw and catch but this work uses |
---|
14 | throw/catch as a particular kind of raise/handle. |
---|
15 | |
---|
16 | \subparagraph{Raise} |
---|
17 | The raise is the starting point for exception handling and usually how |
---|
18 | Some well known examples include the throw statements of \Cpp and Java and |
---|
19 | the raise statement from Python. |
---|
20 | |
---|
21 | For this overview a raise does nothing more kick off the handling of an |
---|
22 | exception, which is called raising the exception. This is inexact but close |
---|
23 | enough for the broad strokes of the overview. |
---|
24 | |
---|
25 | \subparagraph{Handle} |
---|
26 | The purpose of most exception operations is to run some sort of handler that |
---|
27 | contains user code. |
---|
28 | The try statement of \Cpp illistrates the common features |
---|
29 | Handlers have three common features: a region of code they apply to, an |
---|
30 | exception label that describes what exceptions they handle and code to run |
---|
31 | when they handle an exception. |
---|
32 | Each handler can handle exceptions raised in that region that match their |
---|
33 | exception label. Different EHMs will have different rules to pick a handler |
---|
34 | if multipe handlers could be used such as ``best match" or ``first found". |
---|
35 | |
---|
36 | \paragraph{Propagation} |
---|
37 | After an exception is raised comes what is usually the biggest step for the |
---|
38 | EHM, finding and setting up the handler. This can be broken up into three |
---|
39 | different tasks: searching for a handler, matching against the handler and |
---|
40 | installing the handler. |
---|
41 | |
---|
42 | First the EHM must search for possible handlers that could be used to handle |
---|
43 | the exception. Searching is usually independent of the exception that was |
---|
44 | thrown and instead depends on the call stack, the current function, its caller |
---|
45 | and repeating down the stack. |
---|
46 | |
---|
47 | Second it much match the exception with each handler to see which one is the |
---|
48 | best match and hence which one should be used to handle the exception. |
---|
49 | In languages where the best match is the first match these two are often |
---|
50 | intertwined, a match check is preformed immediately after the search finds |
---|
51 | a possible handler. |
---|
52 | |
---|
53 | Third, after a handler is chosen it must be made ready to run. |
---|
54 | What this actually involves can vary widely to fit with the rest of the |
---|
55 | design of the EHM. The installation step might be trivial or it could be |
---|
56 | the most expensive step in handling an exception. The latter tends to be the |
---|
57 | case when stack unwinding is involved. |
---|
58 | |
---|
59 | As an alternate third step if no appropriate handler is found then some sort |
---|
60 | of recovery has to be preformed. This is only required with unchecked |
---|
61 | exceptions as checked exceptions can promise that a handler is found. It also |
---|
62 | is also installing a handler but it is a special default that may be |
---|
63 | installed differently. |
---|
64 | |
---|
65 | \subparagraph{Hierarchy} |
---|
66 | In \CFA the EHM uses a hierarchial system to organise its exceptions. |
---|
67 | This stratagy is borrowed from object-orientated languages where the |
---|
68 | exception hierarchy is a natural extension of the object hierarchy. |
---|
69 | |
---|
70 | Consider the following hierarchy of exceptions: |
---|
71 | \begin{center} |
---|
72 | \setlength{\unitlength}{4000sp}% |
---|
73 | \begin{picture}(1605,612)(2011,-1951) |
---|
74 | \put(2100,-1411){\vector(1, 0){225}} |
---|
75 | \put(3450,-1411){\vector(1, 0){225}} |
---|
76 | \put(3550,-1411){\line(0,-1){225}} |
---|
77 | \put(3550,-1636){\vector(1, 0){150}} |
---|
78 | \put(3550,-1636){\line(0,-1){225}} |
---|
79 | \put(3550,-1861){\vector(1, 0){150}} |
---|
80 | \put(2025,-1490){\makebox(0,0)[rb]{\LstBasicStyle{exception}}} |
---|
81 | \put(2400,-1460){\makebox(0,0)[lb]{\LstBasicStyle{arithmetic}}} |
---|
82 | \put(3750,-1460){\makebox(0,0)[lb]{\LstBasicStyle{underflow}}} |
---|
83 | \put(3750,-1690){\makebox(0,0)[lb]{\LstBasicStyle{overflow}}} |
---|
84 | \put(3750,-1920){\makebox(0,0)[lb]{\LstBasicStyle{zerodivide}}} |
---|
85 | \end{picture}% |
---|
86 | \end{center} |
---|
87 | |
---|
88 | A handler labelled with any given exception can handle exceptions of that |
---|
89 | type or any child type of that exception. The root of the exception hierarchy |
---|
90 | (here \texttt{exception}) acts as a catch-all, leaf types catch single types |
---|
91 | and the exceptions in the middle can be used to catch different groups of |
---|
92 | related exceptions. |
---|
93 | |
---|
94 | This system has some notable advantages, such as multiple levels of grouping, |
---|
95 | the ability for libraries to add new exception types and the isolation |
---|
96 | between different sub-hierarchies. So the design was adapted for a |
---|
97 | non-object-orientated language. |
---|
98 | |
---|
99 | % Could I cite the rational for the Python IO exception rework? |
---|
100 | |
---|
101 | \paragraph{Completion} |
---|
102 | After the handler has finished the entire exception operation has to complete |
---|
103 | and continue executing somewhere else. This step is usually very simple |
---|
104 | both logically and in its implementation as the installation of the handler |
---|
105 | usually does the heavy lifting. |
---|
106 | |
---|
107 | The EHM can return control to many different places. |
---|
108 | However, the most common is after the handler definition and the next most |
---|
109 | common is after the raise. |
---|
110 | |
---|
111 | \paragraph{Communication} |
---|
112 | For effective exception handling, additional information is usually required |
---|
113 | as this base model only communicates the exception's identity. Common |
---|
114 | additional methods of communication are putting fields on an exception and |
---|
115 | allowing a handler to access the lexical scope it is defined in (usually |
---|
116 | a function's local variables). |
---|
117 | |
---|
118 | \paragraph{Other Features} |
---|
119 | Any given exception handling mechanism is free at add other features on top |
---|
120 | of this. This is an overview of the base that all EHMs use but it is not an |
---|
121 | exaustive list of everything an EHM can do. |
---|
122 | |
---|
123 | \section{Virtuals} |
---|
124 | Virtual types and casts are not part of the exception system nor are they |
---|
125 | required for an exception system. But an object-oriented style hierarchy is a |
---|
126 | great way of organizing exceptions so a minimal virtual system has been added |
---|
127 | to \CFA. |
---|
128 | |
---|
129 | The virtual system supports multiple ``trees" of types. Each tree is |
---|
130 | a simple hierarchy with a single root type. Each type in a tree has exactly |
---|
131 | one parent - except for the root type which has zero parents - and any |
---|
132 | number of children. |
---|
133 | Any type that belongs to any of these trees is called a virtual type. |
---|
134 | |
---|
135 | % A type's ancestors are its parent and its parent's ancestors. |
---|
136 | % The root type has no ancestors. |
---|
137 | % A type's decendents are its children and its children's decendents. |
---|
138 | |
---|
139 | Every virtual type also has a list of virtual members. Children inherit |
---|
140 | their parent's list of virtual members but may add new members to it. |
---|
141 | It is important to note that these are virtual members, not virtual methods. |
---|
142 | However as function pointers are allowed they can be used to mimic virtual |
---|
143 | methods as well. |
---|
144 | |
---|
145 | The unique id for the virtual type and all the virtual members are combined |
---|
146 | into a virtual table type. Each virtual type has a pointer to a virtual table |
---|
147 | as a hidden field. |
---|
148 | |
---|
149 | Up until this point the virtual system is a lot like ones found in object- |
---|
150 | orientated languages but this where they diverge. Objects encapsulate a |
---|
151 | single set of behaviours in each type, universally across the entire program, |
---|
152 | and indeed all programs that use that type definition. In this sense the |
---|
153 | types are ``closed" and cannot be altered. |
---|
154 | |
---|
155 | However in \CFA types do not encapsulate any behaviour. Traits are local and |
---|
156 | types can begin to statify a trait, stop satifying a trait or satify the same |
---|
157 | trait in a different way with each new definition. In this sense they are |
---|
158 | ``open" as they can change at any time. This means it is implossible to pick |
---|
159 | a single set of functions that repersent the type. |
---|
160 | |
---|
161 | So we don't try to have a single value. The user can define virtual tables |
---|
162 | which are filled in at their declaration and given a name. Anywhere you can |
---|
163 | see that name you can use that virtual table; even if it is defined locally |
---|
164 | inside a function, although in that case you must respect its lifetime. |
---|
165 | |
---|
166 | An object of a virtual type is ``bound" to a virtual table instance which |
---|
167 | sets the virtual members for that object. The virtual members can be accessed |
---|
168 | through the object. |
---|
169 | |
---|
170 | While much of the virtual infrastructure is created, it is currently only used |
---|
171 | internally for exception handling. The only user-level feature is the virtual |
---|
172 | cast, which is the same as the \Cpp \lstinline[language=C++]|dynamic_cast|. |
---|
173 | \label{p:VirtualCast} |
---|
174 | \begin{cfa} |
---|
175 | (virtual TYPE)EXPRESSION |
---|
176 | \end{cfa} |
---|
177 | Note, the syntax and semantics matches a C-cast, rather than the function-like |
---|
178 | \Cpp syntax for special casts. Both the type of @EXPRESSION@ and @TYPE@ must be |
---|
179 | a pointer to a virtual type. |
---|
180 | The cast dynamically checks if the @EXPRESSION@ type is the same or a sub-type |
---|
181 | of @TYPE@, and if true, returns a pointer to the |
---|
182 | @EXPRESSION@ object, otherwise it returns @0p@ (null pointer). |
---|
183 | |
---|
184 | \section{Exception} |
---|
185 | % Leaving until later, hopefully it can talk about actual syntax instead |
---|
186 | % of my many strange macros. Syntax aside I will also have to talk about the |
---|
187 | % features all exceptions support. |
---|
188 | |
---|
189 | Exceptions are defined by the trait system; there are a series of traits, and |
---|
190 | if a type satisfies them, then it can be used as an exception. The following |
---|
191 | is the base trait all exceptions need to match. |
---|
192 | \begin{cfa} |
---|
193 | trait is_exception(exceptT &, virtualT &) { |
---|
194 | virtualT const & get_exception_vtable(exceptT *); |
---|
195 | }; |
---|
196 | \end{cfa} |
---|
197 | The trait is defined over two types, the exception type and the virtual table |
---|
198 | type. This should be one-to-one, each exception type has only one virtual |
---|
199 | table type and vice versa. The only assertion in the trait is |
---|
200 | @get_exception_vtable@, which takes a pointer of the exception type and |
---|
201 | returns a reference to the virtual table type instance. |
---|
202 | |
---|
203 | The function @get_exception_vtable@ is actually a constant function. |
---|
204 | Regardless of the value passed in (including the null pointer) it should |
---|
205 | return a reference to the virtual table instance for that type. |
---|
206 | The reason it is a function instead of a constant is that it make type |
---|
207 | annotations easier to write as you can use the exception type instead of the |
---|
208 | virtual table type; which usually has a mangled name. |
---|
209 | % Also \CFA's trait system handles functions better than constants and doing |
---|
210 | % it this way reduce the amount of boiler plate we need. |
---|
211 | |
---|
212 | % I did have a note about how it is the programmer's responsibility to make |
---|
213 | % sure the function is implemented correctly. But this is true of every |
---|
214 | % similar system I know of (except Agda's I guess) so I took it out. |
---|
215 | |
---|
216 | There are two more traits for exceptions @is_termination_exception@ and |
---|
217 | @is_resumption_exception@. They are defined as follows: |
---|
218 | |
---|
219 | \begin{cfa} |
---|
220 | trait is_termination_exception( |
---|
221 | exceptT &, virtualT & | is_exception(exceptT, virtualT)) { |
---|
222 | void defaultTerminationHandler(exceptT &); |
---|
223 | }; |
---|
224 | |
---|
225 | trait is_resumption_exception( |
---|
226 | exceptT &, virtualT & | is_exception(exceptT, virtualT)) { |
---|
227 | void defaultResumptionHandler(exceptT &); |
---|
228 | }; |
---|
229 | \end{cfa} |
---|
230 | |
---|
231 | In other words they make sure that a given type and virtual type is an |
---|
232 | exception and defines one of the two default handlers. These default handlers |
---|
233 | are used in the main exception handling operations \see{Exception Handling} |
---|
234 | and their use will be detailed there. |
---|
235 | |
---|
236 | However all three of these traits can be tricky to use directly. |
---|
237 | There is a bit of repetition required but |
---|
238 | the largest issue is that the virtual table type is mangled and not in a user |
---|
239 | facing way. So there are three macros that can be used to wrap these traits |
---|
240 | when you need to refer to the names: |
---|
241 | @IS_EXCEPTION@, @IS_TERMINATION_EXCEPTION@ and @IS_RESUMPTION_EXCEPTION@. |
---|
242 | |
---|
243 | All take one or two arguments. The first argument is the name of the |
---|
244 | exception type. Its unmangled and mangled form are passed to the trait. |
---|
245 | The second (optional) argument is a parenthesized list of polymorphic |
---|
246 | arguments. This argument should only with polymorphic exceptions and the |
---|
247 | list will be passed to both types. |
---|
248 | In the current set-up the base name and the polymorphic arguments have to |
---|
249 | match so these macros can be used without losing flexibility. |
---|
250 | |
---|
251 | For example consider a function that is polymorphic over types that have a |
---|
252 | defined arithmetic exception: |
---|
253 | \begin{cfa} |
---|
254 | forall(Num | IS_EXCEPTION(Arithmetic, (Num))) |
---|
255 | void some_math_function(Num & left, Num & right); |
---|
256 | \end{cfa} |
---|
257 | |
---|
258 | \section{Exception Handling} |
---|
259 | \CFA provides two kinds of exception handling, termination and resumption. |
---|
260 | These twin operations are the core of the exception handling mechanism and |
---|
261 | are the reason for the features of exceptions. |
---|
262 | This section will cover the general patterns shared by the two operations and |
---|
263 | then go on to cover the details each individual operation. |
---|
264 | |
---|
265 | Both operations follow the same set of steps to do their operation. They both |
---|
266 | start with the user preforming a throw on an exception. |
---|
267 | Then there is the search for a handler, if one is found than the exception |
---|
268 | is caught and the handler is run. After that control returns to normal |
---|
269 | execution. |
---|
270 | |
---|
271 | If the search fails a default handler is run and then control |
---|
272 | returns to normal execution immediately. That is where the default handlers |
---|
273 | @defaultTermiationHandler@ and @defaultResumptionHandler@ are used. |
---|
274 | |
---|
275 | \subsection{Termination} |
---|
276 | \label{s:Termination} |
---|
277 | |
---|
278 | Termination handling is more familiar kind and used in most programming |
---|
279 | languages with exception handling. |
---|
280 | It is dynamic, non-local goto. If a throw is successful then the stack will |
---|
281 | be unwound and control will (usually) continue in a different function on |
---|
282 | the call stack. They are commonly used when an error has occurred and recovery |
---|
283 | is impossible in the current function. |
---|
284 | |
---|
285 | % (usually) Control can continue in the current function but then a different |
---|
286 | % control flow construct should be used. |
---|
287 | |
---|
288 | A termination throw is started with the @throw@ statement: |
---|
289 | \begin{cfa} |
---|
290 | throw EXPRESSION; |
---|
291 | \end{cfa} |
---|
292 | The expression must return a reference to a termination exception, where the |
---|
293 | termination exception is any type that satisfies @is_termination_exception@ |
---|
294 | at the call site. |
---|
295 | Through \CFA's trait system the functions in the traits are passed into the |
---|
296 | throw code. A new @defaultTerminationHandler@ can be defined in any scope to |
---|
297 | change the throw's behavior (see below). |
---|
298 | |
---|
299 | The throw will copy the provided exception into managed memory. It is the |
---|
300 | user's responsibility to ensure the original exception is cleaned up if the |
---|
301 | stack is unwound (allocating it on the stack should be sufficient). |
---|
302 | |
---|
303 | Then the exception system searches the stack using the copied exception. |
---|
304 | It starts starts from the throw and proceeds to the base of the stack, |
---|
305 | from callee to caller. |
---|
306 | At each stack frame, a check is made for resumption handlers defined by the |
---|
307 | @catch@ clauses of a @try@ statement. |
---|
308 | \begin{cfa} |
---|
309 | try { |
---|
310 | GUARDED_BLOCK |
---|
311 | } catch (EXCEPTION_TYPE$\(_1\)$ * NAME$\(_1\)$) { |
---|
312 | HANDLER_BLOCK$\(_1\)$ |
---|
313 | } catch (EXCEPTION_TYPE$\(_2\)$ * NAME$\(_2\)$) { |
---|
314 | HANDLER_BLOCK$\(_2\)$ |
---|
315 | } |
---|
316 | \end{cfa} |
---|
317 | When viewed on its own a try statement will simply execute the statements in |
---|
318 | @GUARDED_BLOCK@ and when those are finished the try statement finishes. |
---|
319 | |
---|
320 | However, while the guarded statements are being executed, including any |
---|
321 | functions they invoke, all the handlers following the try block are now |
---|
322 | or any functions invoked from those |
---|
323 | statements, throws an exception, and the exception |
---|
324 | is not handled by a try statement further up the stack, the termination |
---|
325 | handlers are searched for a matching exception type from top to bottom. |
---|
326 | |
---|
327 | Exception matching checks the representation of the thrown exception-type is |
---|
328 | the same or a descendant type of the exception types in the handler clauses. If |
---|
329 | it is the same of a descendant of @EXCEPTION_TYPE@$_i$ then @NAME@$_i$ is |
---|
330 | bound to a pointer to the exception and the statements in @HANDLER_BLOCK@$_i$ |
---|
331 | are executed. If control reaches the end of the handler, the exception is |
---|
332 | freed and control continues after the try statement. |
---|
333 | |
---|
334 | If no handler is found during the search then the default handler is run. |
---|
335 | Through \CFA's trait system the best match at the throw sight will be used. |
---|
336 | This function is run and is passed the copied exception. After the default |
---|
337 | handler is run control continues after the throw statement. |
---|
338 | |
---|
339 | There is a global @defaultTerminationHandler@ that cancels the current stack |
---|
340 | with the copied exception. However it is generic over all exception types so |
---|
341 | new default handlers can be defined for different exception types and so |
---|
342 | different exception types can have different default handlers. |
---|
343 | |
---|
344 | \subsection{Resumption} |
---|
345 | \label{s:Resumption} |
---|
346 | |
---|
347 | Resumption exception handling is a less common form than termination but is |
---|
348 | just as old~\cite{Goodenough75} and is in some sense simpler. |
---|
349 | It is a dynamic, non-local function call. If the throw is successful a |
---|
350 | closure will be taken from up the stack and executed, after which the throwing |
---|
351 | function will continue executing. |
---|
352 | These are most often used when an error occurred and if the error is repaired |
---|
353 | then the function can continue. |
---|
354 | |
---|
355 | A resumption raise is started with the @throwResume@ statement: |
---|
356 | \begin{cfa} |
---|
357 | throwResume EXPRESSION; |
---|
358 | \end{cfa} |
---|
359 | The semantics of the @throwResume@ statement are like the @throw@, but the |
---|
360 | expression has return a reference a type that satisfies the trait |
---|
361 | @is_resumption_exception@. The assertions from this trait are available to |
---|
362 | the exception system while handling the exception. |
---|
363 | |
---|
364 | At run-time, no copies are made. As the stack is not unwound the exception and |
---|
365 | any values on the stack will remain in scope while the resumption is handled. |
---|
366 | |
---|
367 | Then the exception system searches the stack using the provided exception. |
---|
368 | It starts starts from the throw and proceeds to the base of the stack, |
---|
369 | from callee to caller. |
---|
370 | At each stack frame, a check is made for resumption handlers defined by the |
---|
371 | @catchResume@ clauses of a @try@ statement. |
---|
372 | \begin{cfa} |
---|
373 | try { |
---|
374 | GUARDED_BLOCK |
---|
375 | } catchResume (EXCEPTION_TYPE$\(_1\)$ * NAME$\(_1\)$) { |
---|
376 | HANDLER_BLOCK$\(_1\)$ |
---|
377 | } catchResume (EXCEPTION_TYPE$\(_2\)$ * NAME$\(_2\)$) { |
---|
378 | HANDLER_BLOCK$\(_2\)$ |
---|
379 | } |
---|
380 | \end{cfa} |
---|
381 | If the handlers are not involved in a search this will simply execute the |
---|
382 | @GUARDED_BLOCK@ and then continue to the next statement. |
---|
383 | Its purpose is to add handlers onto the stack. |
---|
384 | (Note, termination and resumption handlers may be intermixed in a @try@ |
---|
385 | statement but the kind of throw must be the same as the handler for it to be |
---|
386 | considered as a possible match.) |
---|
387 | |
---|
388 | If a search for a resumption handler reaches a try block it will check each |
---|
389 | @catchResume@ clause, top-to-bottom. |
---|
390 | At each handler if the thrown exception is or is a child type of |
---|
391 | @EXCEPTION_TYPE@$_i$ then the a pointer to the exception is bound to |
---|
392 | @NAME@$_i$ and then @HANDLER_BLOCK@$_i$ is executed. After the block is |
---|
393 | finished control will return to the @throwResume@ statement. |
---|
394 | |
---|
395 | Like termination, if no resumption handler is found, the default handler |
---|
396 | visible at the throw statement is called. It will use the best match at the |
---|
397 | call sight according to \CFA's overloading rules. The default handler is |
---|
398 | passed the exception given to the throw. When the default handler finishes |
---|
399 | execution continues after the throw statement. |
---|
400 | |
---|
401 | There is a global @defaultResumptionHandler@ is polymorphic over all |
---|
402 | termination exceptions and preforms a termination throw on the exception. |
---|
403 | The @defaultTerminationHandler@ for that throw is matched at the original |
---|
404 | throw statement (the resumption @throwResume@) and it can be customized by |
---|
405 | introducing a new or better match as well. |
---|
406 | |
---|
407 | % \subsubsection? |
---|
408 | |
---|
409 | A key difference between resumption and termination is that resumption does |
---|
410 | not unwind the stack. A side effect that is that when a handler is matched |
---|
411 | and run it's try block (the guarded statements) and every try statement |
---|
412 | searched before it are still on the stack. This can lead to the recursive |
---|
413 | resumption problem. |
---|
414 | |
---|
415 | The recursive resumption problem is any situation where a resumption handler |
---|
416 | ends up being called while it is running. |
---|
417 | Consider a trivial case: |
---|
418 | \begin{cfa} |
---|
419 | try { |
---|
420 | throwResume (E &){}; |
---|
421 | } catchResume(E *) { |
---|
422 | throwResume (E &){}; |
---|
423 | } |
---|
424 | \end{cfa} |
---|
425 | When this code is executed the guarded @throwResume@ will throw, start a |
---|
426 | search and match the handler in the @catchResume@ clause. This will be |
---|
427 | call and placed on the stack on top of the try-block. The second throw then |
---|
428 | throws and will search the same try block and put call another instance of the |
---|
429 | same handler leading to an infinite loop. |
---|
430 | |
---|
431 | This situation is trivial and easy to avoid, but much more complex cycles |
---|
432 | can form with multiple handlers and different exception types. |
---|
433 | |
---|
434 | To prevent all of these cases we mask sections of the stack, or equivalently |
---|
435 | the try statements on the stack, so that the resumption search skips over |
---|
436 | them and continues with the next unmasked section of the stack. |
---|
437 | |
---|
438 | A section of the stack is marked when it is searched to see if it contains |
---|
439 | a handler for an exception and unmarked when that exception has been handled |
---|
440 | or the search was completed without finding a handler. |
---|
441 | |
---|
442 | % This might need a diagram. But it is an important part of the justification |
---|
443 | % of the design of the traversal order. |
---|
444 | \begin{verbatim} |
---|
445 | throwResume2 ----------. |
---|
446 | | | |
---|
447 | generated from handler | |
---|
448 | | | |
---|
449 | handler | |
---|
450 | | | |
---|
451 | throwResume1 -----. : |
---|
452 | | | : |
---|
453 | try | : search skip |
---|
454 | | | : |
---|
455 | catchResume <----' : |
---|
456 | | | |
---|
457 | \end{verbatim} |
---|
458 | |
---|
459 | The rules can be remembered as thinking about what would be searched in |
---|
460 | termination. So when a throw happens in a handler; a termination handler |
---|
461 | skips everything from the original throw to the original catch because that |
---|
462 | part of the stack has been unwound, a resumption handler skips the same |
---|
463 | section of stack because it has been masked. |
---|
464 | A throw in a default handler will preform the same search as the original |
---|
465 | throw because; for termination nothing has been unwound, for resumption |
---|
466 | the mask will be the same. |
---|
467 | |
---|
468 | The symmetry with termination is why this pattern was picked. Other patterns, |
---|
469 | such as marking just the handlers that caught, also work but lack the |
---|
470 | symmetry which means there is more to remember. |
---|
471 | |
---|
472 | \section{Conditional Catch} |
---|
473 | Both termination and resumption handler clauses can be given an additional |
---|
474 | condition to further control which exceptions they handle: |
---|
475 | \begin{cfa} |
---|
476 | catch (EXCEPTION_TYPE * NAME ; CONDITION) |
---|
477 | \end{cfa} |
---|
478 | First, the same semantics is used to match the exception type. Second, if the |
---|
479 | exception matches, @CONDITION@ is executed. The condition expression may |
---|
480 | reference all names in scope at the beginning of the try block and @NAME@ |
---|
481 | introduced in the handler clause. If the condition is true, then the handler |
---|
482 | matches. Otherwise, the exception search continues as if the exception type |
---|
483 | did not match. |
---|
484 | \begin{cfa} |
---|
485 | try { |
---|
486 | f1 = open( ... ); |
---|
487 | f2 = open( ... ); |
---|
488 | ... |
---|
489 | } catch( IOFailure * f ; fd( f ) == f1 ) { |
---|
490 | // only handle IO failure for f1 |
---|
491 | } |
---|
492 | \end{cfa} |
---|
493 | Note, catching @IOFailure@, checking for @f1@ in the handler, and re-raising the |
---|
494 | exception if not @f1@ is different because the re-raise does not examine any of |
---|
495 | remaining handlers in the current try statement. |
---|
496 | |
---|
497 | \section{Rethrowing} |
---|
498 | \colour{red}{From Andrew: I recomend we talk about why the language doesn't |
---|
499 | have rethrows/reraises instead.} |
---|
500 | |
---|
501 | \label{s:Rethrowing} |
---|
502 | Within the handler block or functions called from the handler block, it is |
---|
503 | possible to reraise the most recently caught exception with @throw@ or |
---|
504 | @throwResume@, respectively. |
---|
505 | \begin{cfa} |
---|
506 | try { |
---|
507 | ... |
---|
508 | } catch( ... ) { |
---|
509 | ... throw; |
---|
510 | } catchResume( ... ) { |
---|
511 | ... throwResume; |
---|
512 | } |
---|
513 | \end{cfa} |
---|
514 | The only difference between a raise and a reraise is that reraise does not |
---|
515 | create a new exception; instead it continues using the current exception, \ie |
---|
516 | no allocation and copy. However the default handler is still set to the one |
---|
517 | visible at the raise point, and hence, for termination could refer to data that |
---|
518 | is part of an unwound stack frame. To prevent this problem, a new default |
---|
519 | handler is generated that does a program-level abort. |
---|
520 | |
---|
521 | \section{Finally Clauses} |
---|
522 | Finally clauses are used to preform unconditional clean-up when leaving a |
---|
523 | scope. They are placed at the end of a try statement: |
---|
524 | \begin{cfa} |
---|
525 | try { |
---|
526 | GUARDED_BLOCK |
---|
527 | } ... // any number or kind of handler clauses |
---|
528 | ... finally { |
---|
529 | FINALLY_BLOCK |
---|
530 | } |
---|
531 | \end{cfa} |
---|
532 | The @FINALLY_BLOCK@ is executed when the try statement is removed from the |
---|
533 | stack, including when the @GUARDED_BLOCK@ finishes, any termination handler |
---|
534 | finishes or during an unwind. |
---|
535 | The only time the block is not executed is if the program is exited before |
---|
536 | the stack is unwound. |
---|
537 | |
---|
538 | Execution of the finally block should always finish, meaning control runs off |
---|
539 | the end of the block. This requirement ensures always continues as if the |
---|
540 | finally clause is not present, \ie finally is for cleanup not changing control |
---|
541 | flow. Because of this requirement, local control flow out of the finally block |
---|
542 | is forbidden. The compiler precludes any @break@, @continue@, @fallthru@ or |
---|
543 | @return@ that causes control to leave the finally block. Other ways to leave |
---|
544 | the finally block, such as a long jump or termination are much harder to check, |
---|
545 | and at best requiring additional run-time overhead, and so are mealy |
---|
546 | discouraged. |
---|
547 | |
---|
548 | Not all languages with exceptions have finally clauses. Notably \Cpp does |
---|
549 | without it as descructors serve a similar role. Although destructors and |
---|
550 | finally clauses can be used in many of the same areas they have their own |
---|
551 | use cases like top-level functions and lambda functions with closures. |
---|
552 | Destructors take a bit more work to set up but are much easier to reuse while |
---|
553 | finally clauses are good for once offs and can include local information. |
---|
554 | |
---|
555 | \section{Cancellation} |
---|
556 | Cancellation is a stack-level abort, which can be thought of as as an |
---|
557 | uncatchable termination. It unwinds the entirety of the current stack, and if |
---|
558 | possible forwards the cancellation exception to a different stack. |
---|
559 | |
---|
560 | Cancellation is not an exception operation like termination or resumption. |
---|
561 | There is no special statement for starting a cancellation; instead the standard |
---|
562 | library function @cancel_stack@ is called passing an exception. Unlike a |
---|
563 | throw, this exception is not used in matching only to pass information about |
---|
564 | the cause of the cancellation. |
---|
565 | (This also means matching cannot fail so there is no default handler either.) |
---|
566 | |
---|
567 | After @cancel_stack@ is called the exception is copied into the exception |
---|
568 | handling mechanism's memory. Then the entirety of the current stack is |
---|
569 | unwound. After that it depends one which stack is being cancelled. |
---|
570 | \begin{description} |
---|
571 | \item[Main Stack:] |
---|
572 | The main stack is the one used by the program main at the start of execution, |
---|
573 | and is the only stack in a sequential program. Even in a concurrent program |
---|
574 | the main stack is only dependent on the environment that started the program. |
---|
575 | Hence, when the main stack is cancelled there is nowhere else in the program |
---|
576 | to notify. After the stack is unwound, there is a program-level abort. |
---|
577 | |
---|
578 | \item[Thread Stack:] |
---|
579 | A thread stack is created for a @thread@ object or object that satisfies the |
---|
580 | @is_thread@ trait. A thread only has two points of communication that must |
---|
581 | happen: start and join. As the thread must be running to perform a |
---|
582 | cancellation, it must occur after start and before join, so join is used |
---|
583 | for communication here. |
---|
584 | After the stack is unwound, the thread halts and waits for |
---|
585 | another thread to join with it. The joining thread checks for a cancellation, |
---|
586 | and if present, resumes exception @ThreadCancelled@. |
---|
587 | |
---|
588 | There is a subtle difference between the explicit join (@join@ function) and |
---|
589 | implicit join (from a destructor call). The explicit join takes the default |
---|
590 | handler (@defaultResumptionHandler@) from its calling context, which is used if |
---|
591 | the exception is not caught. The implicit join does a program abort instead. |
---|
592 | |
---|
593 | This semantics is for safety. If an unwind is triggered while another unwind |
---|
594 | is underway only one of them can proceed as they both want to ``consume'' the |
---|
595 | stack. Letting both try to proceed leads to very undefined behaviour. |
---|
596 | Both termination and cancellation involve unwinding and, since the default |
---|
597 | @defaultResumptionHandler@ preforms a termination that could more easily |
---|
598 | happen in an implicate join inside a destructor. So there is an error message |
---|
599 | and an abort instead. |
---|
600 | \todo{Perhaps have a more general disucssion of unwind collisions before |
---|
601 | this point.} |
---|
602 | |
---|
603 | The recommended way to avoid the abort is to handle the initial resumption |
---|
604 | from the implicate join. If required you may put an explicate join inside a |
---|
605 | finally clause to disable the check and use the local |
---|
606 | @defaultResumptionHandler@ instead. |
---|
607 | |
---|
608 | \item[Coroutine Stack:] A coroutine stack is created for a @coroutine@ object |
---|
609 | or object that satisfies the @is_coroutine@ trait. A coroutine only knows of |
---|
610 | two other coroutines, its starter and its last resumer. Of the two the last |
---|
611 | resumer has the tightest coupling to the coroutine it activated and the most |
---|
612 | up-to-date information. |
---|
613 | |
---|
614 | Hence, cancellation of the active coroutine is forwarded to the last resumer |
---|
615 | after the stack is unwound. When the resumer restarts, it resumes exception |
---|
616 | @CoroutineCancelled@, which is polymorphic over the coroutine type and has a |
---|
617 | pointer to the cancelled coroutine. |
---|
618 | |
---|
619 | The resume function also has an assertion that the @defaultResumptionHandler@ |
---|
620 | for the exception. So it will use the default handler like a regular throw. |
---|
621 | \end{description} |
---|