[88e0080] | 1 | Design of Exceptions and EHM in Cforall: |
---|
| 2 | |
---|
| 3 | |
---|
[465ed18] | 4 | Exception Instances: |
---|
| 5 | Currently, exceptions are integers (like errno). |
---|
[88e0080] | 6 | |
---|
[465ed18] | 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. |
---|
[88e0080] | 10 | |
---|
[465ed18] | 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. |
---|
[88e0080] | 15 | |
---|
[465ed18] | 16 | The shared functionality across exceptions has not been finalized, but will |
---|
| 17 | probably include things like human readable descriptions and default handlers. |
---|
[88e0080] | 18 | |
---|
| 19 | |
---|
[465ed18] | 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. |
---|
[88e0080] | 25 | |
---|
[465ed18] | 26 | Syntax |
---|
| 27 | "throw" exception ";" |
---|
| 28 | "throwResume" exception ";" |
---|
[88e0080] | 29 | |
---|
[465ed18] | 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. |
---|
[88e0080] | 32 | |
---|
[465ed18] | 33 | Syntax |
---|
| 34 | "throwResume" exception "_At" target ";" |
---|
[88e0080] | 35 | |
---|
[465ed18] | 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. |
---|
[88e0080] | 40 | |
---|
| 41 | |
---|
[465ed18] | 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. |
---|
[88e0080] | 45 | |
---|
[465ed18] | 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 | )? |
---|
[88e0080] | 56 | |
---|
[465ed18] | 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. |
---|
[88e0080] | 62 | |
---|
[465ed18] | 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. |
---|
[88e0080] | 65 | |
---|
| 66 | |
---|
[465ed18] | 67 | Implementation Overview: |
---|
[88e0080] | 68 | |
---|
[465ed18] | 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. |
---|
[88e0080] | 73 | |
---|
[465ed18] | 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. |
---|
[88e0080] | 80 | |
---|
[465ed18] | 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. |
---|
[88e0080] | 85 | |
---|
[465ed18] | 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. |
---|
[88e0080] | 88 | |
---|
| 89 | |
---|
[465ed18] | 90 | Alternative Error Handling: Return Unions |
---|
[88e0080] | 91 | |
---|
[465ed18] | 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. |
---|
[88e0080] | 95 | |
---|
[465ed18] | 96 | For errors that are usually handled quite close to where they occur, these |
---|
| 97 | can replace exceptions. |
---|
[88e0080] | 98 | |
---|
[465ed18] | 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. |
---|
[88e0080] | 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 | |
---|
[465ed18] | 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: |
---|
[88e0080] | 112 | forall(otype T, otype E | exception(E)) |
---|
| 113 | T get_or_throw (Result(T, E) * this) { |
---|
[465ed18] | 114 | if (has_value(this)) { |
---|
| 115 | return get_value(this); |
---|
[88e0080] | 116 | } else { |
---|
[465ed18] | 117 | throw get_error(this); |
---|
[88e0080] | 118 | } |
---|
| 119 | } |
---|