[4706098c] | 1 | \chapter{Exception Features} |
---|
| 2 | |
---|
| 3 | This chapter covers the design and user interface of the \CFA |
---|
[4260566] | 4 | exception-handling mechanism (EHM). % or exception system. |
---|
| 5 | |
---|
[f6106a6] | 6 | We will begin with an overview of EHMs in general. It is not a strict |
---|
| 7 | definition of all EHMs nor an exaustive list of all possible features. |
---|
| 8 | However it does cover the most common structure and features found in them. |
---|
| 9 | |
---|
[4260566] | 10 | % We should cover what is an exception handling mechanism and what is an |
---|
| 11 | % exception before this. Probably in the introduction. Some of this could |
---|
| 12 | % move there. |
---|
| 13 | \paragraph{Raise / Handle} |
---|
| 14 | An exception operation has two main parts: raise and handle. |
---|
| 15 | These terms are sometimes also known as throw and catch but this work uses |
---|
| 16 | throw/catch as a particular kind of raise/handle. |
---|
[f6106a6] | 17 | These are the two parts that the user will write themselves and may |
---|
| 18 | be the only two pieces of the EHM that have any syntax in the language. |
---|
[4260566] | 19 | |
---|
| 20 | \subparagraph{Raise} |
---|
[f6106a6] | 21 | The raise is the starting point for exception handling. It marks the beginning |
---|
[df24d37] | 22 | of exception handling by raising an excepion, which passes it to |
---|
[f6106a6] | 23 | the EHM. |
---|
[4260566] | 24 | |
---|
[f6106a6] | 25 | Some well known examples include the @throw@ statements of \Cpp and Java and |
---|
[299b8b2] | 26 | the \code{Python}{raise} statement from Python. In real systems a raise may |
---|
| 27 | preform some other work (such as memory management) but for the |
---|
| 28 | purposes of this overview that can be ignored. |
---|
[4260566] | 29 | |
---|
| 30 | \subparagraph{Handle} |
---|
[f6106a6] | 31 | The purpose of most exception operations is to run some user code to handle |
---|
| 32 | that exception. This code is given, with some other information, in a handler. |
---|
| 33 | |
---|
| 34 | A handler has three common features: the previously mentioned user code, a |
---|
| 35 | region of code they cover and an exception label/condition that matches |
---|
| 36 | certain exceptions. |
---|
| 37 | Only raises inside the covered region and raising exceptions that match the |
---|
| 38 | label can be handled by a given handler. |
---|
| 39 | Different EHMs will have different rules to pick a handler |
---|
[de47a9d] | 40 | if multipe handlers could be used such as ``best match" or ``first found". |
---|
[4260566] | 41 | |
---|
[f6106a6] | 42 | The @try@ statements of \Cpp, Java and Python are common examples. All three |
---|
| 43 | also show another common feature of handlers, they are grouped by the covered |
---|
| 44 | region. |
---|
| 45 | |
---|
[4260566] | 46 | \paragraph{Propagation} |
---|
[de47a9d] | 47 | After an exception is raised comes what is usually the biggest step for the |
---|
[f6106a6] | 48 | EHM: finding and setting up the handler. The propogation from raise to |
---|
| 49 | handler can be broken up into three different tasks: searching for a handler, |
---|
| 50 | matching against the handler and installing the handler. |
---|
[de47a9d] | 51 | |
---|
[f6106a6] | 52 | \subparagraph{Searching} |
---|
| 53 | The EHM begins by searching for handlers that might be used to handle |
---|
[de47a9d] | 54 | the exception. Searching is usually independent of the exception that was |
---|
[f6106a6] | 55 | thrown as it looks for handlers that have the raise site in their covered |
---|
| 56 | region. |
---|
| 57 | This includes handlers in the current function, as well as any in callers |
---|
| 58 | on the stack that have the function call in their covered region. |
---|
| 59 | |
---|
| 60 | \subparagraph{Matching} |
---|
| 61 | Each handler found has to be matched with the raised exception. The exception |
---|
| 62 | label defines a condition that be use used with exception and decides if |
---|
| 63 | there is a match or not. |
---|
| 64 | |
---|
| 65 | In languages where the first match is used this step is intertwined with |
---|
| 66 | searching, a match check is preformed immediately after the search finds |
---|
[4260566] | 67 | a possible handler. |
---|
| 68 | |
---|
[f6106a6] | 69 | \subparagraph{Installing} |
---|
| 70 | After a handler is chosen it must be made ready to run. |
---|
| 71 | The implementation can vary widely to fit with the rest of the |
---|
[de47a9d] | 72 | design of the EHM. The installation step might be trivial or it could be |
---|
[4260566] | 73 | the most expensive step in handling an exception. The latter tends to be the |
---|
| 74 | case when stack unwinding is involved. |
---|
[de47a9d] | 75 | |
---|
[f6106a6] | 76 | If a matching handler is not guarantied to be found the EHM will need a |
---|
| 77 | different course of action here in the cases where no handler matches. |
---|
| 78 | This is only required with unchecked exceptions as checked exceptions |
---|
| 79 | (such as in Java) can make than guaranty. |
---|
| 80 | This different action can also be installing a handler but it is usually an |
---|
| 81 | implicat and much more general one. |
---|
[4260566] | 82 | |
---|
| 83 | \subparagraph{Hierarchy} |
---|
[f6106a6] | 84 | A common way to organize exceptions is in a hierarchical structure. |
---|
| 85 | This is especially true in object-orientated languages where the |
---|
[4260566] | 86 | exception hierarchy is a natural extension of the object hierarchy. |
---|
| 87 | |
---|
| 88 | Consider the following hierarchy of exceptions: |
---|
[4706098c] | 89 | \begin{center} |
---|
[6a8208cb] | 90 | \input{exception-hierarchy} |
---|
[4706098c] | 91 | \end{center} |
---|
[de47a9d] | 92 | |
---|
[4260566] | 93 | A handler labelled with any given exception can handle exceptions of that |
---|
| 94 | type or any child type of that exception. The root of the exception hierarchy |
---|
[299b8b2] | 95 | (here \code{C}{exception}) acts as a catch-all, leaf types catch single types |
---|
[4260566] | 96 | and the exceptions in the middle can be used to catch different groups of |
---|
| 97 | related exceptions. |
---|
| 98 | |
---|
| 99 | This system has some notable advantages, such as multiple levels of grouping, |
---|
[de47a9d] | 100 | the ability for libraries to add new exception types and the isolation |
---|
[f6106a6] | 101 | between different sub-hierarchies. |
---|
| 102 | This design is used in \CFA even though it is not a object-orientated |
---|
[a6c45c6] | 103 | language; so different tools are used to create the hierarchy. |
---|
[4260566] | 104 | |
---|
| 105 | % Could I cite the rational for the Python IO exception rework? |
---|
| 106 | |
---|
| 107 | \paragraph{Completion} |
---|
[de47a9d] | 108 | After the handler has finished the entire exception operation has to complete |
---|
[f6106a6] | 109 | and continue executing somewhere else. This step is usually simple, |
---|
| 110 | both logically and in its implementation, as the installation of the handler |
---|
| 111 | is usually set up to do most of the work. |
---|
[de47a9d] | 112 | |
---|
[f6106a6] | 113 | The EHM can return control to many different places, |
---|
| 114 | the most common are after the handler definition and after the raise. |
---|
[4260566] | 115 | |
---|
| 116 | \paragraph{Communication} |
---|
[f6106a6] | 117 | For effective exception handling, additional information is usually passed |
---|
| 118 | from the raise to the handler. |
---|
| 119 | So far only communication of the exceptions' identity has been covered. |
---|
| 120 | A common method is putting fields into the exception instance and giving the |
---|
| 121 | handler access to them. |
---|
[4260566] | 122 | |
---|
| 123 | \section{Virtuals} |
---|
[f6106a6] | 124 | Virtual types and casts are not part of \CFA's EHM nor are they required for |
---|
[a6c45c6] | 125 | any EHM. |
---|
| 126 | However the \CFA uses a hierarchy built with the virtual system as the basis |
---|
| 127 | for exceptions and exception matching. |
---|
| 128 | |
---|
| 129 | The virtual system would have ideally been part of \CFA before the work |
---|
| 130 | on exception handling began, but unfortunately it was not. |
---|
| 131 | Because of this only the features and framework needed for the EHM were |
---|
| 132 | designed and implemented. Other features were considered to ensure that |
---|
| 133 | the structure could accomidate other desirable features but they were not |
---|
| 134 | implemented. |
---|
| 135 | The rest of this section will only discuss the finalized portion of the |
---|
| 136 | virtual system. |
---|
[4260566] | 137 | |
---|
| 138 | The virtual system supports multiple ``trees" of types. Each tree is |
---|
| 139 | a simple hierarchy with a single root type. Each type in a tree has exactly |
---|
[f6106a6] | 140 | one parent -- except for the root type which has zero parents -- and any |
---|
[4260566] | 141 | number of children. |
---|
| 142 | Any type that belongs to any of these trees is called a virtual type. |
---|
| 143 | |
---|
| 144 | % A type's ancestors are its parent and its parent's ancestors. |
---|
| 145 | % The root type has no ancestors. |
---|
[de47a9d] | 146 | % A type's decendents are its children and its children's decendents. |
---|
[4260566] | 147 | |
---|
[de47a9d] | 148 | Every virtual type also has a list of virtual members. Children inherit |
---|
| 149 | their parent's list of virtual members but may add new members to it. |
---|
[f6106a6] | 150 | It is important to note that these are virtual members, not virtual methods |
---|
| 151 | of object-orientated programming, and can be of any type. |
---|
[c21f5a9] | 152 | \CFA still supports virtual methods as a special case of virtual members. |
---|
| 153 | Function pointers that take a pointer to the virtual type will be modified |
---|
| 154 | with each level of inheritance so that refers to the new type. |
---|
| 155 | This means an object can always be passed to a function in its virtual table |
---|
| 156 | as if it were a method. |
---|
[4260566] | 157 | |
---|
[f6106a6] | 158 | Each virtual type has a unique id. |
---|
| 159 | This unique id and all the virtual members are combined |
---|
[de47a9d] | 160 | into a virtual table type. Each virtual type has a pointer to a virtual table |
---|
[4260566] | 161 | as a hidden field. |
---|
| 162 | |
---|
[f6106a6] | 163 | Up until this point the virtual system is similar to ones found in |
---|
| 164 | object-orientated languages but this where \CFA diverges. Objects encapsulate a |
---|
[08e75215] | 165 | single set of behaviours in each type, universally across the entire program, |
---|
[de47a9d] | 166 | and indeed all programs that use that type definition. In this sense the |
---|
[08e75215] | 167 | types are ``closed" and cannot be altered. |
---|
[de47a9d] | 168 | |
---|
[f6106a6] | 169 | In \CFA types do not encapsulate any behaviour. Traits are local and |
---|
[de47a9d] | 170 | types can begin to statify a trait, stop satifying a trait or satify the same |
---|
[f6106a6] | 171 | trait in a different way at any lexical location in the program. |
---|
| 172 | In this sense they are ``open" as they can change at any time. This means it |
---|
| 173 | is implossible to pick a single set of functions that repersent the type's |
---|
| 174 | implementation across the program. |
---|
| 175 | |
---|
| 176 | \CFA side-steps this issue by not having a single virtual table for each |
---|
| 177 | type. A user can define virtual tables which are filled in at their |
---|
| 178 | declaration and given a name. Anywhere that name is visible, even if it was |
---|
| 179 | defined locally inside a function (although that means it will not have a |
---|
| 180 | static lifetime), it can be used. |
---|
| 181 | Specifically, a virtual type is ``bound" to a virtual table which |
---|
[08e75215] | 182 | sets the virtual members for that object. The virtual members can be accessed |
---|
| 183 | through the object. |
---|
[4706098c] | 184 | |
---|
| 185 | While much of the virtual infrastructure is created, it is currently only used |
---|
| 186 | internally for exception handling. The only user-level feature is the virtual |
---|
[299b8b2] | 187 | cast, which is the same as the \Cpp \code{C++}{dynamic_cast}. |
---|
[7eb6eb5] | 188 | \label{p:VirtualCast} |
---|
[4706098c] | 189 | \begin{cfa} |
---|
[4a36b344] | 190 | (virtual TYPE)EXPRESSION |
---|
[4706098c] | 191 | \end{cfa} |
---|
[29c9b23] | 192 | Note, the syntax and semantics matches a C-cast, rather than the function-like |
---|
| 193 | \Cpp syntax for special casts. Both the type of @EXPRESSION@ and @TYPE@ must be |
---|
| 194 | a pointer to a virtual type. |
---|
[de47a9d] | 195 | The cast dynamically checks if the @EXPRESSION@ type is the same or a sub-type |
---|
[29c9b23] | 196 | of @TYPE@, and if true, returns a pointer to the |
---|
[4706098c] | 197 | @EXPRESSION@ object, otherwise it returns @0p@ (null pointer). |
---|
| 198 | |
---|
| 199 | \section{Exception} |
---|
[4a36b344] | 200 | % Leaving until later, hopefully it can talk about actual syntax instead |
---|
| 201 | % of my many strange macros. Syntax aside I will also have to talk about the |
---|
| 202 | % features all exceptions support. |
---|
| 203 | |
---|
[4706098c] | 204 | Exceptions are defined by the trait system; there are a series of traits, and |
---|
[1c1c180] | 205 | if a type satisfies them, then it can be used as an exception. The following |
---|
[4706098c] | 206 | is the base trait all exceptions need to match. |
---|
| 207 | \begin{cfa} |
---|
| 208 | trait is_exception(exceptT &, virtualT &) { |
---|
[a6c45c6] | 209 | // Numerous imaginary assertions. |
---|
[02b73ea] | 210 | }; |
---|
[4706098c] | 211 | \end{cfa} |
---|
[29c9b23] | 212 | The trait is defined over two types, the exception type and the virtual table |
---|
[a6c45c6] | 213 | type. Each exception type should have but a single virtual table type. |
---|
| 214 | Now there are no actual assertions in this trait because the trait system |
---|
| 215 | actually can't express them (adding such assertions would be part of |
---|
| 216 | completing the virtual system). The imaginary assertions would probably come |
---|
| 217 | from a trait defined by the virtual system, and state that the exception type |
---|
| 218 | is a virtual type, is a decendent of @exception_t@ (the base exception type) |
---|
| 219 | and note its virtual table type. |
---|
[29c9b23] | 220 | |
---|
| 221 | % I did have a note about how it is the programmer's responsibility to make |
---|
| 222 | % sure the function is implemented correctly. But this is true of every |
---|
[de47a9d] | 223 | % similar system I know of (except Agda's I guess) so I took it out. |
---|
| 224 | |
---|
[f6106a6] | 225 | There are two more traits for exceptions defined as follows: |
---|
[4706098c] | 226 | \begin{cfa} |
---|
[02b73ea] | 227 | trait is_termination_exception( |
---|
[4706098c] | 228 | exceptT &, virtualT & | is_exception(exceptT, virtualT)) { |
---|
[29c9b23] | 229 | void defaultTerminationHandler(exceptT &); |
---|
[02b73ea] | 230 | }; |
---|
| 231 | |
---|
| 232 | trait is_resumption_exception( |
---|
[4706098c] | 233 | exceptT &, virtualT & | is_exception(exceptT, virtualT)) { |
---|
[29c9b23] | 234 | void defaultResumptionHandler(exceptT &); |
---|
[02b73ea] | 235 | }; |
---|
[4706098c] | 236 | \end{cfa} |
---|
[f6106a6] | 237 | Both traits ensure a pair of types are an exception type and its virtual table |
---|
| 238 | and defines one of the two default handlers. The default handlers are used |
---|
[df24d37] | 239 | as fallbacks and are discussed in detail in \vref{s:ExceptionHandling}. |
---|
[de47a9d] | 240 | |
---|
[f6106a6] | 241 | However, all three of these traits can be tricky to use directly. |
---|
| 242 | While there is a bit of repetition required, |
---|
[de47a9d] | 243 | the largest issue is that the virtual table type is mangled and not in a user |
---|
[f6106a6] | 244 | facing way. So these three macros are provided to wrap these traits to |
---|
| 245 | simplify referring to the names: |
---|
[29c9b23] | 246 | @IS_EXCEPTION@, @IS_TERMINATION_EXCEPTION@ and @IS_RESUMPTION_EXCEPTION@. |
---|
[1830a86] | 247 | |
---|
[f6106a6] | 248 | All three take one or two arguments. The first argument is the name of the |
---|
| 249 | exception type. The macro passes its unmangled and mangled form to the trait. |
---|
[1830a86] | 250 | The second (optional) argument is a parenthesized list of polymorphic |
---|
[f6106a6] | 251 | arguments. This argument is only used with polymorphic exceptions and the |
---|
| 252 | list is be passed to both types. |
---|
| 253 | In the current set-up, the two types always have the same polymorphic |
---|
| 254 | arguments so these macros can be used without losing flexibility. |
---|
[29c9b23] | 255 | |
---|
| 256 | For example consider a function that is polymorphic over types that have a |
---|
| 257 | defined arithmetic exception: |
---|
| 258 | \begin{cfa} |
---|
[de47a9d] | 259 | forall(Num | IS_EXCEPTION(Arithmetic, (Num))) |
---|
[29c9b23] | 260 | void some_math_function(Num & left, Num & right); |
---|
| 261 | \end{cfa} |
---|
[4706098c] | 262 | |
---|
[1830a86] | 263 | \section{Exception Handling} |
---|
[f6106a6] | 264 | \label{s:ExceptionHandling} |
---|
| 265 | \CFA provides two kinds of exception handling: termination and resumption. |
---|
| 266 | These twin operations are the core of \CFA's exception handling mechanism. |
---|
[de47a9d] | 267 | This section will cover the general patterns shared by the two operations and |
---|
| 268 | then go on to cover the details each individual operation. |
---|
| 269 | |
---|
[f6106a6] | 270 | Both operations follow the same set of steps. |
---|
| 271 | Both start with the user preforming a raise on an exception. |
---|
| 272 | Then the exception propogates up the stack. |
---|
| 273 | If a handler is found the exception is caught and the handler is run. |
---|
| 274 | After that control returns to normal execution. |
---|
[de47a9d] | 275 | If the search fails a default handler is run and then control |
---|
[f6106a6] | 276 | returns to normal execution after the raise. |
---|
| 277 | |
---|
| 278 | This general description covers what the two kinds have in common. |
---|
| 279 | Differences include how propogation is preformed, where exception continues |
---|
| 280 | after an exception is caught and handled and which default handler is run. |
---|
[1830a86] | 281 | |
---|
[4706098c] | 282 | \subsection{Termination} |
---|
| 283 | \label{s:Termination} |
---|
[f6106a6] | 284 | Termination handling is the familiar kind and used in most programming |
---|
[1830a86] | 285 | languages with exception handling. |
---|
[f6106a6] | 286 | It is dynamic, non-local goto. If the raised exception is matched and |
---|
| 287 | handled the stack is unwound and control will (usually) continue the function |
---|
| 288 | on the call stack that defined the handler. |
---|
| 289 | Termination is commonly used when an error has occurred and recovery is |
---|
| 290 | impossible locally. |
---|
[1830a86] | 291 | |
---|
| 292 | % (usually) Control can continue in the current function but then a different |
---|
| 293 | % control flow construct should be used. |
---|
[4706098c] | 294 | |
---|
[f6106a6] | 295 | A termination raise is started with the @throw@ statement: |
---|
[4706098c] | 296 | \begin{cfa} |
---|
[4a36b344] | 297 | throw EXPRESSION; |
---|
[4706098c] | 298 | \end{cfa} |
---|
[29c9b23] | 299 | The expression must return a reference to a termination exception, where the |
---|
[f6106a6] | 300 | termination exception is any type that satisfies the trait |
---|
| 301 | @is_termination_exception@ at the call site. |
---|
| 302 | Through \CFA's trait system the trait functions are implicity passed into the |
---|
| 303 | throw code and the EHM. |
---|
| 304 | A new @defaultTerminationHandler@ can be defined in any scope to |
---|
[de47a9d] | 305 | change the throw's behavior (see below). |
---|
| 306 | |
---|
[f6106a6] | 307 | The throw will copy the provided exception into managed memory to ensure |
---|
| 308 | the exception is not destroyed if the stack is unwound. |
---|
| 309 | It is the user's responsibility to ensure the original exception is cleaned |
---|
| 310 | up wheither the stack is unwound or not. Allocating it on the stack is |
---|
| 311 | usually sufficient. |
---|
[de47a9d] | 312 | |
---|
[f6106a6] | 313 | Then propogation starts with the search. \CFA uses a ``first match" rule so |
---|
| 314 | matching is preformed with the copied exception as the search continues. |
---|
| 315 | It starts from the throwing function and proceeds to the base of the stack, |
---|
[1830a86] | 316 | from callee to caller. |
---|
[de47a9d] | 317 | At each stack frame, a check is made for resumption handlers defined by the |
---|
[1830a86] | 318 | @catch@ clauses of a @try@ statement. |
---|
[4706098c] | 319 | \begin{cfa} |
---|
[4a36b344] | 320 | try { |
---|
[4706098c] | 321 | GUARDED_BLOCK |
---|
[f6106a6] | 322 | } catch (EXCEPTION_TYPE$\(_1\)$ * [NAME$\(_1\)$]) { |
---|
[4706098c] | 323 | HANDLER_BLOCK$\(_1\)$ |
---|
[f6106a6] | 324 | } catch (EXCEPTION_TYPE$\(_2\)$ * [NAME$\(_2\)$]) { |
---|
[4706098c] | 325 | HANDLER_BLOCK$\(_2\)$ |
---|
[4a36b344] | 326 | } |
---|
[4706098c] | 327 | \end{cfa} |
---|
[f6106a6] | 328 | When viewed on its own, a try statement will simply execute the statements |
---|
| 329 | in @GUARDED_BLOCK@ and when those are finished the try statement finishes. |
---|
[de47a9d] | 330 | |
---|
| 331 | However, while the guarded statements are being executed, including any |
---|
[f6106a6] | 332 | invoked functions, all the handlers in the statement are now on the search |
---|
| 333 | path. If a termination exception is thrown and not handled further up the |
---|
| 334 | stack they will be matched against the exception. |
---|
| 335 | |
---|
| 336 | Exception matching checks the handler in each catch clause in the order |
---|
| 337 | they appear, top to bottom. If the representation of the thrown exception type |
---|
| 338 | is the same or a descendant of @EXCEPTION_TYPE@$_i$ then @NAME@$_i$ |
---|
| 339 | (if provided) is |
---|
[29c9b23] | 340 | bound to a pointer to the exception and the statements in @HANDLER_BLOCK@$_i$ |
---|
| 341 | are executed. If control reaches the end of the handler, the exception is |
---|
[de47a9d] | 342 | freed and control continues after the try statement. |
---|
[4706098c] | 343 | |
---|
[f6106a6] | 344 | If no termination handler is found during the search then the default handler |
---|
| 345 | (@defaultTerminationHandler@) is run. |
---|
[de47a9d] | 346 | Through \CFA's trait system the best match at the throw sight will be used. |
---|
| 347 | This function is run and is passed the copied exception. After the default |
---|
| 348 | handler is run control continues after the throw statement. |
---|
[1830a86] | 349 | |
---|
[f6106a6] | 350 | There is a global @defaultTerminationHandler@ that is polymorphic over all |
---|
| 351 | exception types. Since it is so general a more specific handler can be |
---|
| 352 | defined and will be used for those types, effectively overriding the handler |
---|
| 353 | for particular exception type. |
---|
| 354 | The global default termination handler performs a cancellation |
---|
[df24d37] | 355 | (see \vref{s:Cancellation}) on the current stack with the copied exception. |
---|
[4706098c] | 356 | |
---|
| 357 | \subsection{Resumption} |
---|
| 358 | \label{s:Resumption} |
---|
| 359 | |
---|
[f6106a6] | 360 | Resumption exception handling is less common than termination but is |
---|
| 361 | just as old~\cite{Goodenough75} and is simpler in many ways. |
---|
| 362 | It is a dynamic, non-local function call. If the raised exception is |
---|
| 363 | matched a closure will be taken from up the stack and executed, |
---|
| 364 | after which the raising function will continue executing. |
---|
[de47a9d] | 365 | These are most often used when an error occurred and if the error is repaired |
---|
| 366 | then the function can continue. |
---|
[8483c39a] | 367 | |
---|
[4706098c] | 368 | A resumption raise is started with the @throwResume@ statement: |
---|
| 369 | \begin{cfa} |
---|
[4a36b344] | 370 | throwResume EXPRESSION; |
---|
[4706098c] | 371 | \end{cfa} |
---|
[f6106a6] | 372 | It works much the same way as the termination throw. |
---|
| 373 | The expression must return a reference to a resumption exception, |
---|
| 374 | where the resumption exception is any type that satisfies the trait |
---|
| 375 | @is_resumption_exception@ at the call site. |
---|
| 376 | The assertions from this trait are available to |
---|
[1830a86] | 377 | the exception system while handling the exception. |
---|
[29c9b23] | 378 | |
---|
[f6106a6] | 379 | At run-time, no exception copy is made. |
---|
| 380 | As the stack is not unwound the exception and |
---|
[de47a9d] | 381 | any values on the stack will remain in scope while the resumption is handled. |
---|
[4706098c] | 382 | |
---|
[f6106a6] | 383 | The EHM then begins propogation. The search starts from the raise in the |
---|
| 384 | resuming function and proceeds to the base of the stack, from callee to caller. |
---|
[1830a86] | 385 | At each stack frame, a check is made for resumption handlers defined by the |
---|
| 386 | @catchResume@ clauses of a @try@ statement. |
---|
[4706098c] | 387 | \begin{cfa} |
---|
[4a36b344] | 388 | try { |
---|
[4706098c] | 389 | GUARDED_BLOCK |
---|
[f6106a6] | 390 | } catchResume (EXCEPTION_TYPE$\(_1\)$ * [NAME$\(_1\)$]) { |
---|
[4706098c] | 391 | HANDLER_BLOCK$\(_1\)$ |
---|
[f6106a6] | 392 | } catchResume (EXCEPTION_TYPE$\(_2\)$ * [NAME$\(_2\)$]) { |
---|
[4706098c] | 393 | HANDLER_BLOCK$\(_2\)$ |
---|
[4a36b344] | 394 | } |
---|
[4706098c] | 395 | \end{cfa} |
---|
[f6106a6] | 396 | % I wonder if there would be some good central place for this. |
---|
| 397 | Note that termination handlers and resumption handlers may be used together |
---|
| 398 | in a single try statement, intermixing @catch@ and @catchResume@ freely. |
---|
| 399 | Each type of handler will only interact with exceptions from the matching |
---|
| 400 | type of raise. |
---|
| 401 | When a try statement is executed it simply executes the statements in the |
---|
| 402 | @GUARDED_BLOCK@ and then finishes. |
---|
| 403 | |
---|
| 404 | However, while the guarded statements are being executed, including any |
---|
| 405 | invoked functions, all the handlers in the statement are now on the search |
---|
| 406 | path. If a resumption exception is reported and not handled further up the |
---|
| 407 | stack they will be matched against the exception. |
---|
| 408 | |
---|
| 409 | Exception matching checks the handler in each catch clause in the order |
---|
| 410 | they appear, top to bottom. If the representation of the thrown exception type |
---|
| 411 | is the same or a descendant of @EXCEPTION_TYPE@$_i$ then @NAME@$_i$ |
---|
| 412 | (if provided) is bound to a pointer to the exception and the statements in |
---|
| 413 | @HANDLER_BLOCK@$_i$ are executed. |
---|
| 414 | If control reaches the end of the handler, execution continues after the |
---|
| 415 | the raise statement that raised the handled exception. |
---|
[de47a9d] | 416 | |
---|
| 417 | Like termination, if no resumption handler is found, the default handler |
---|
| 418 | visible at the throw statement is called. It will use the best match at the |
---|
| 419 | call sight according to \CFA's overloading rules. The default handler is |
---|
| 420 | passed the exception given to the throw. When the default handler finishes |
---|
[f6106a6] | 421 | execution continues after the raise statement. |
---|
[de47a9d] | 422 | |
---|
| 423 | There is a global @defaultResumptionHandler@ is polymorphic over all |
---|
| 424 | termination exceptions and preforms a termination throw on the exception. |
---|
[f6106a6] | 425 | The @defaultTerminationHandler@ for that raise is matched at the original |
---|
| 426 | raise statement (the resumption @throwResume@) and it can be customized by |
---|
[1830a86] | 427 | introducing a new or better match as well. |
---|
| 428 | |
---|
[f6106a6] | 429 | \subsubsection{Resumption Marking} |
---|
[df24d37] | 430 | \label{s:ResumptionMarking} |
---|
[1830a86] | 431 | A key difference between resumption and termination is that resumption does |
---|
[de47a9d] | 432 | not unwind the stack. A side effect that is that when a handler is matched |
---|
| 433 | and run it's try block (the guarded statements) and every try statement |
---|
[1830a86] | 434 | searched before it are still on the stack. This can lead to the recursive |
---|
| 435 | resumption problem. |
---|
| 436 | |
---|
| 437 | The recursive resumption problem is any situation where a resumption handler |
---|
| 438 | ends up being called while it is running. |
---|
| 439 | Consider a trivial case: |
---|
| 440 | \begin{cfa} |
---|
| 441 | try { |
---|
| 442 | throwResume (E &){}; |
---|
| 443 | } catchResume(E *) { |
---|
| 444 | throwResume (E &){}; |
---|
| 445 | } |
---|
| 446 | \end{cfa} |
---|
[de47a9d] | 447 | When this code is executed the guarded @throwResume@ will throw, start a |
---|
| 448 | search and match the handler in the @catchResume@ clause. This will be |
---|
| 449 | call and placed on the stack on top of the try-block. The second throw then |
---|
| 450 | throws and will search the same try block and put call another instance of the |
---|
[1830a86] | 451 | same handler leading to an infinite loop. |
---|
| 452 | |
---|
[de47a9d] | 453 | This situation is trivial and easy to avoid, but much more complex cycles |
---|
[1830a86] | 454 | can form with multiple handlers and different exception types. |
---|
| 455 | |
---|
[f6106a6] | 456 | To prevent all of these cases we mark try statements on the stack. |
---|
| 457 | A try statement is marked when a match check is preformed with it and an |
---|
| 458 | exception. The statement will be unmarked when the handling of that exception |
---|
| 459 | is completed or the search completes without finding a handler. |
---|
| 460 | While a try statement is marked its handlers are never matched, effectify |
---|
| 461 | skipping over it to the next try statement. |
---|
[4a36b344] | 462 | |
---|
[6a8208cb] | 463 | \begin{center} |
---|
| 464 | \input{stack-marking} |
---|
| 465 | \end{center} |
---|
[de47a9d] | 466 | |
---|
[f6106a6] | 467 | These rules mirror what happens with termination. |
---|
| 468 | When a termination throw happens in a handler the search will not look at |
---|
| 469 | any handlers from the original throw to the original catch because that |
---|
| 470 | part of the stack has been unwound. |
---|
| 471 | A resumption raise in the same situation wants to search the entire stack, |
---|
| 472 | but it will not try to match the exception with try statements in the section |
---|
| 473 | that would have been unwound as they are marked. |
---|
[4706098c] | 474 | |
---|
[f6106a6] | 475 | The symmetry between resumption termination is why this pattern was picked. |
---|
| 476 | Other patterns, such as marking just the handlers that caught, also work but |
---|
[a6c45c6] | 477 | lack the symmetry means there are more rules to remember. |
---|
[4706098c] | 478 | |
---|
| 479 | \section{Conditional Catch} |
---|
[de47a9d] | 480 | Both termination and resumption handler clauses can be given an additional |
---|
| 481 | condition to further control which exceptions they handle: |
---|
[4706098c] | 482 | \begin{cfa} |
---|
[f6106a6] | 483 | catch (EXCEPTION_TYPE * [NAME] ; CONDITION) |
---|
[4706098c] | 484 | \end{cfa} |
---|
| 485 | First, the same semantics is used to match the exception type. Second, if the |
---|
| 486 | exception matches, @CONDITION@ is executed. The condition expression may |
---|
[de47a9d] | 487 | reference all names in scope at the beginning of the try block and @NAME@ |
---|
[1c1c180] | 488 | introduced in the handler clause. If the condition is true, then the handler |
---|
[1830a86] | 489 | matches. Otherwise, the exception search continues as if the exception type |
---|
| 490 | did not match. |
---|
[f6106a6] | 491 | |
---|
| 492 | The condition matching allows finer matching by allowing the match to check |
---|
| 493 | more kinds of information than just the exception type. |
---|
[4706098c] | 494 | \begin{cfa} |
---|
| 495 | try { |
---|
[f6106a6] | 496 | handle1 = open( f1, ... ); |
---|
| 497 | handle2 = open( f2, ... ); |
---|
| 498 | handle3 = open( f3, ... ); |
---|
[4706098c] | 499 | ... |
---|
[de47a9d] | 500 | } catch( IOFailure * f ; fd( f ) == f1 ) { |
---|
[f6106a6] | 501 | // Only handle IO failure for f1. |
---|
| 502 | } catch( IOFailure * f ; fd( f ) == f3 ) { |
---|
| 503 | // Only handle IO failure for f3. |
---|
[4706098c] | 504 | } |
---|
[f6106a6] | 505 | // Can't handle a failure relating to f2 here. |
---|
[4706098c] | 506 | \end{cfa} |
---|
[f6106a6] | 507 | In this example the file that experianced the IO error is used to decide |
---|
| 508 | which handler should be run, if any at all. |
---|
| 509 | |
---|
| 510 | \begin{comment} |
---|
| 511 | % I know I actually haven't got rid of them yet, but I'm going to try |
---|
| 512 | % to write it as if I had and see if that makes sense: |
---|
| 513 | \section{Reraising} |
---|
| 514 | \label{s:Reraising} |
---|
[4706098c] | 515 | Within the handler block or functions called from the handler block, it is |
---|
| 516 | possible to reraise the most recently caught exception with @throw@ or |
---|
[1830a86] | 517 | @throwResume@, respectively. |
---|
[4706098c] | 518 | \begin{cfa} |
---|
[29c9b23] | 519 | try { |
---|
| 520 | ... |
---|
| 521 | } catch( ... ) { |
---|
[1830a86] | 522 | ... throw; |
---|
[4706098c] | 523 | } catchResume( ... ) { |
---|
[1830a86] | 524 | ... throwResume; |
---|
[4706098c] | 525 | } |
---|
| 526 | \end{cfa} |
---|
| 527 | The only difference between a raise and a reraise is that reraise does not |
---|
| 528 | create a new exception; instead it continues using the current exception, \ie |
---|
| 529 | no allocation and copy. However the default handler is still set to the one |
---|
| 530 | visible at the raise point, and hence, for termination could refer to data that |
---|
| 531 | is part of an unwound stack frame. To prevent this problem, a new default |
---|
| 532 | handler is generated that does a program-level abort. |
---|
[f6106a6] | 533 | \end{comment} |
---|
| 534 | |
---|
| 535 | \subsection{Comparison with Reraising} |
---|
| 536 | A more popular way to allow handlers to match in more detail is to reraise |
---|
| 537 | the exception after it has been caught if it could not be handled here. |
---|
| 538 | On the surface these two features seem interchangable. |
---|
| 539 | |
---|
| 540 | If we used @throw;@ to start a termination reraise then these two statements |
---|
| 541 | would have the same behaviour: |
---|
| 542 | \begin{cfa} |
---|
| 543 | try { |
---|
| 544 | do_work_may_throw(); |
---|
| 545 | } catch(exception_t * exc ; can_handle(exc)) { |
---|
| 546 | handle(exc); |
---|
| 547 | } |
---|
| 548 | \end{cfa} |
---|
| 549 | |
---|
| 550 | \begin{cfa} |
---|
| 551 | try { |
---|
| 552 | do_work_may_throw(); |
---|
| 553 | } catch(exception_t * exc) { |
---|
| 554 | if (can_handle(exc)) { |
---|
| 555 | handle(exc); |
---|
| 556 | } else { |
---|
| 557 | throw; |
---|
| 558 | } |
---|
| 559 | } |
---|
| 560 | \end{cfa} |
---|
| 561 | If there are further handlers after this handler only the first version will |
---|
[a6c45c6] | 562 | check them. If multiple handlers on a single try block that could handle the |
---|
| 563 | same exception the translations get more complex but they are equivilantly |
---|
[f6106a6] | 564 | powerful. |
---|
| 565 | |
---|
| 566 | Until stack unwinding comes into the picture. In termination handling, a |
---|
| 567 | conditional catch happens before the stack is unwound, but a reraise happens |
---|
| 568 | afterwards. Normally this might only cause you to loose some debug |
---|
| 569 | information you could get from a stack trace (and that can be side stepped |
---|
| 570 | entirely by collecting information during the unwind). But for \CFA there is |
---|
| 571 | another issue, if the exception isn't handled the default handler should be |
---|
| 572 | run at the site of the original raise. |
---|
| 573 | |
---|
| 574 | There are two problems with this: the site of the original raise doesn't |
---|
| 575 | exist anymore and the default handler might not exist anymore. The site will |
---|
| 576 | always be removed as part of the unwinding, often with the entirety of the |
---|
| 577 | function it was in. The default handler could be a stack allocated nested |
---|
| 578 | function removed during the unwind. |
---|
| 579 | |
---|
| 580 | This means actually trying to pretend the catch didn't happening, continuing |
---|
| 581 | the original raise instead of starting a new one, is infeasible. |
---|
| 582 | That is the expected behaviour for most languages and we can't replicate |
---|
| 583 | that behaviour. |
---|
[4a36b344] | 584 | |
---|
| 585 | \section{Finally Clauses} |
---|
[f6106a6] | 586 | \label{s:FinallyClauses} |
---|
[de47a9d] | 587 | Finally clauses are used to preform unconditional clean-up when leaving a |
---|
[f6106a6] | 588 | scope and are placed at the end of a try statement after any handler clauses: |
---|
[4706098c] | 589 | \begin{cfa} |
---|
[4a36b344] | 590 | try { |
---|
[4706098c] | 591 | GUARDED_BLOCK |
---|
[29c9b23] | 592 | } ... // any number or kind of handler clauses |
---|
| 593 | ... finally { |
---|
[4706098c] | 594 | FINALLY_BLOCK |
---|
[4a36b344] | 595 | } |
---|
[4706098c] | 596 | \end{cfa} |
---|
[29c9b23] | 597 | The @FINALLY_BLOCK@ is executed when the try statement is removed from the |
---|
[1830a86] | 598 | stack, including when the @GUARDED_BLOCK@ finishes, any termination handler |
---|
[de47a9d] | 599 | finishes or during an unwind. |
---|
[29c9b23] | 600 | The only time the block is not executed is if the program is exited before |
---|
[1830a86] | 601 | the stack is unwound. |
---|
[4706098c] | 602 | |
---|
| 603 | Execution of the finally block should always finish, meaning control runs off |
---|
[f6106a6] | 604 | the end of the block. This requirement ensures control always continues as if |
---|
| 605 | the finally clause is not present, \ie finally is for cleanup not changing |
---|
| 606 | control flow. |
---|
| 607 | Because of this requirement, local control flow out of the finally block |
---|
[1c1c180] | 608 | is forbidden. The compiler precludes any @break@, @continue@, @fallthru@ or |
---|
[4706098c] | 609 | @return@ that causes control to leave the finally block. Other ways to leave |
---|
| 610 | the finally block, such as a long jump or termination are much harder to check, |
---|
[f6106a6] | 611 | and at best requiring additional run-time overhead, and so are only |
---|
[1830a86] | 612 | discouraged. |
---|
| 613 | |
---|
[f6106a6] | 614 | Not all languages with unwinding have finally clauses. Notably \Cpp does |
---|
[de47a9d] | 615 | without it as descructors serve a similar role. Although destructors and |
---|
| 616 | finally clauses can be used in many of the same areas they have their own |
---|
[1830a86] | 617 | use cases like top-level functions and lambda functions with closures. |
---|
| 618 | Destructors take a bit more work to set up but are much easier to reuse while |
---|
[f6106a6] | 619 | finally clauses are good for one-off uses and |
---|
| 620 | can easily include local information. |
---|
[4a36b344] | 621 | |
---|
| 622 | \section{Cancellation} |
---|
[f6106a6] | 623 | \label{s:Cancellation} |
---|
[de47a9d] | 624 | Cancellation is a stack-level abort, which can be thought of as as an |
---|
[f6106a6] | 625 | uncatchable termination. It unwinds the entire current stack, and if |
---|
[de47a9d] | 626 | possible forwards the cancellation exception to a different stack. |
---|
[4706098c] | 627 | |
---|
[29c9b23] | 628 | Cancellation is not an exception operation like termination or resumption. |
---|
[4706098c] | 629 | There is no special statement for starting a cancellation; instead the standard |
---|
[1c1c180] | 630 | library function @cancel_stack@ is called passing an exception. Unlike a |
---|
[f6106a6] | 631 | raise, this exception is not used in matching only to pass information about |
---|
[4706098c] | 632 | the cause of the cancellation. |
---|
[f6106a6] | 633 | (This also means matching cannot fail so there is no default handler.) |
---|
[4706098c] | 634 | |
---|
[f6106a6] | 635 | After @cancel_stack@ is called the exception is copied into the EHM's memory |
---|
| 636 | and the current stack is |
---|
[1830a86] | 637 | unwound. After that it depends one which stack is being cancelled. |
---|
[a6c45c6] | 638 | |
---|
| 639 | \paragraph{Main Stack} |
---|
[4706098c] | 640 | The main stack is the one used by the program main at the start of execution, |
---|
[f6106a6] | 641 | and is the only stack in a sequential program. |
---|
| 642 | After the main stack is unwound there is a program-level abort. |
---|
| 643 | |
---|
| 644 | There are two reasons for this. The first is that it obviously had to do this |
---|
| 645 | in a sequential program as there is nothing else to notify and the simplicity |
---|
| 646 | of keeping the same behaviour in sequential and concurrent programs is good. |
---|
| 647 | Also, even in concurrent programs there is no stack that an innate connection |
---|
| 648 | to, so it would have be explicitly managed. |
---|
[4706098c] | 649 | |
---|
[a6c45c6] | 650 | \paragraph{Thread Stack} |
---|
[f6106a6] | 651 | A thread stack is created for a \CFA @thread@ object or object that satisfies |
---|
| 652 | the @is_thread@ trait. |
---|
| 653 | After a thread stack is unwound there exception is stored until another |
---|
| 654 | thread attempts to join with it. Then the exception @ThreadCancelled@, |
---|
| 655 | which stores a reference to the thread and to the exception passed to the |
---|
| 656 | cancellation, is reported from the join. |
---|
| 657 | There is one difference between an explicit join (with the @join@ function) |
---|
| 658 | and an implicit join (from a destructor call). The explicit join takes the |
---|
| 659 | default handler (@defaultResumptionHandler@) from its calling context while |
---|
| 660 | the implicit join provides its own which does a program abort if the |
---|
| 661 | @ThreadCancelled@ exception cannot be handled. |
---|
| 662 | |
---|
| 663 | Communication is done at join because a thread only has to have to points of |
---|
| 664 | communication with other threads: start and join. |
---|
| 665 | Since a thread must be running to perform a cancellation (and cannot be |
---|
| 666 | cancelled from another stack), the cancellation must be after start and |
---|
| 667 | before the join. So join is the one that we will use. |
---|
| 668 | |
---|
| 669 | % TODO: Find somewhere to discuss unwind collisions. |
---|
| 670 | The difference between the explicit and implicit join is for safety and |
---|
| 671 | debugging. It helps prevent unwinding collisions by avoiding throwing from |
---|
| 672 | a destructor and prevents cascading the error across multiple threads if |
---|
| 673 | the user is not equipped to deal with it. |
---|
| 674 | Also you can always add an explicit join if that is the desired behaviour. |
---|
| 675 | |
---|
[a6c45c6] | 676 | \paragraph{Coroutine Stack} |
---|
[f6106a6] | 677 | A coroutine stack is created for a @coroutine@ object or object that |
---|
| 678 | satisfies the @is_coroutine@ trait. |
---|
| 679 | After a coroutine stack is unwound control returns to the resume function |
---|
| 680 | that most recently resumed it. The resume statement reports a |
---|
| 681 | @CoroutineCancelled@ exception, which contains a references to the cancelled |
---|
| 682 | coroutine and the exception used to cancel it. |
---|
| 683 | The resume function also takes the @defaultResumptionHandler@ from the |
---|
| 684 | caller's context and passes it to the internal report. |
---|
| 685 | |
---|
| 686 | A coroutine knows of two other coroutines, its starter and its last resumer. |
---|
| 687 | The starter has a much more distant connection while the last resumer just |
---|
| 688 | (in terms of coroutine state) called resume on this coroutine, so the message |
---|
| 689 | is passed to the latter. |
---|