1 | Design of Exceptions and EHM in Cforall:
|
---|
2 |
|
---|
3 |
|
---|
4 | Exception Instances:
|
---|
5 | Currently, exceptions are integers (like errno).
|
---|
6 |
|
---|
7 | They are planned to be the new "tagged structures", which allows them to
|
---|
8 | exist in a simple hierarchy which shared functionality throughout. However
|
---|
9 | the tagged structures are not yet implemented so that will wait.
|
---|
10 |
|
---|
11 | A single built in exception is at the top of the hierarchy and all other
|
---|
12 | exceptions are its children. When you match against an exception, you match
|
---|
13 | for an exception and its children, so the top of the hierarchy is used as a
|
---|
14 | catch-all option.
|
---|
15 |
|
---|
16 | The shared functionality across exceptions has not been finalized, but will
|
---|
17 | probably include things like human readable descriptions and default handlers.
|
---|
18 |
|
---|
19 |
|
---|
20 | Throwing:
|
---|
21 | There are currently two kinds of throws, "throw" for termination and
|
---|
22 | "throwResume" for resumption. Both keywords can be used to create a throw
|
---|
23 | statement. The kind of throw decides what handlers may catch the exception
|
---|
24 | and weither control flow can return to the throw site.
|
---|
25 |
|
---|
26 | Syntax
|
---|
27 | "throw" exception ";"
|
---|
28 | "throwResume" exception ";"
|
---|
29 |
|
---|
30 | Non-local throws are allowed for resumption only. A target is an object with
|
---|
31 | a stack, with which it may propagate and handle the exception.
|
---|
32 |
|
---|
33 | Syntax
|
---|
34 | "throwResume" exception "_At" target ";"
|
---|
35 |
|
---|
36 | Termination throws unwind the stack until a handler is reached, control moves
|
---|
37 | onwards from the end of the handler. Resumption throws do not unwind, if a
|
---|
38 | handler is found and control will return to the throw after the exception is
|
---|
39 | handled.
|
---|
40 |
|
---|
41 |
|
---|
42 | Catching:
|
---|
43 | The catch and handle of an exception is preformed with a try statement, which
|
---|
44 | also can have finally clauses to exceute on exit from the scope.
|
---|
45 |
|
---|
46 | Syntax
|
---|
47 | "try"
|
---|
48 | try-block
|
---|
49 | ( ("catch" | "catchResume")
|
---|
50 | "(" exception_type [identifier] [";" conditional_expression] ")"
|
---|
51 | catch-block
|
---|
52 | )*
|
---|
53 | ("finally"
|
---|
54 | finally-block
|
---|
55 | )?
|
---|
56 |
|
---|
57 | Either at least 1 handler clause or the finally clasue must be given on each
|
---|
58 | try block. Each handler clause handles 1 of the two types of throws. Each
|
---|
59 | handler also specifies a type of exception it handles, and will handle all
|
---|
60 | children exceptions as well. In addition, a conditional expression which, if
|
---|
61 | included, must be true for the handler to catch the exception.
|
---|
62 |
|
---|
63 | The two types of handlers may be intermixed. Multiple handlers catching the
|
---|
64 | same type may also be used, to allow for fallbacks on false conditionals.
|
---|
65 |
|
---|
66 |
|
---|
67 | Implementation Overview:
|
---|
68 |
|
---|
69 | The implementation has two main parts. The first is just a collection of the
|
---|
70 | support definitions we need, the data types and functions used within the
|
---|
71 | exception handling code. Second is a translation from Cforall code to C code
|
---|
72 | that uses those definitions to throw, catch and handle exceptions.
|
---|
73 |
|
---|
74 | Termination handlers call a specially annotated function, passing it inner
|
---|
75 | functions that act as the varius sub-blocks. Termination throws use the
|
---|
76 | unwind library that checks the underlying code for those annotations. Each
|
---|
77 | time one is found some magic is used to check for a matching handler, if one
|
---|
78 | is found control goes to the special function which excecutes the handler and
|
---|
79 | returns.
|
---|
80 |
|
---|
81 | Resumption handlers maintain a linked list of stack allocated nodes that have
|
---|
82 | the handler functions attached. Throwing a resumption exception traverses this
|
---|
83 | list, and calls each handler, the handlers handle the exception if they can
|
---|
84 | and return if they did or not.
|
---|
85 |
|
---|
86 | Finally clauses just use stack cleanup to force a nested function, which has
|
---|
87 | the code from the finally clause, to execute when we leave that section.
|
---|
88 |
|
---|
89 |
|
---|
90 | Alternative Error Handling: Return Unions
|
---|
91 |
|
---|
92 | Return unions (Maybe and Result), are types that can encode a success or
|
---|
93 | other result in a single value. Maybe stores a value or nothing, Result stores
|
---|
94 | a value or an error.
|
---|
95 |
|
---|
96 | For errors that are usually handled quite close to where they occur, these
|
---|
97 | can replace exceptions.
|
---|
98 |
|
---|
99 | They tend to be faster and require similar or less amounts of code to handle.
|
---|
100 | However they can slow down the normal path with some extra conditionals and
|
---|
101 | can mix the normal and exceptional control flow path. If handling the error
|
---|
102 | is simple, and happens relatively frequently, this might be prefered but in
|
---|
103 | other cases it just hurts speed and readability.
|
---|
104 |
|
---|
105 | In short, these errors seem to be more effective when errors are likely and
|
---|
106 | immediate. High failure operations, especially ones with failures that can
|
---|
107 | be handled locally, might be better off using these instead of exceptions.
|
---|
108 |
|
---|
109 | Also the return unions could use exceptions as well. Getting the improper
|
---|
110 | side of a return union might throw an exception. Or we can provide helpers
|
---|
111 | for results withe exceptions as in:
|
---|
112 | forall(otype T, otype E | exception(E))
|
---|
113 | T get_or_throw (Result(T, E) * this) {
|
---|
114 | if (has_value(this)) {
|
---|
115 | return get_value(this);
|
---|
116 | } else {
|
---|
117 | throw get_error(this);
|
---|
118 | }
|
---|
119 | }
|
---|