[4706098c] | 1 | \chapter{Exception Features} |
---|
[553f8abe] | 2 | \label{c:features} |
---|
[4706098c] | 3 | |
---|
[4aba055] | 4 | This chapter covers the design and user interface of the \CFA EHM |
---|
| 5 | and begins with a general overview of EHMs. It is not a strict |
---|
| 6 | definition of all EHMs nor an exhaustive list of all possible features. |
---|
[9cdfa5fb] | 7 | However, it does cover the most common structure and features found in them. |
---|
[f6106a6] | 8 | |
---|
[4aba055] | 9 | \section{Overview of EHMs} |
---|
[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. |
---|
[4aba055] | 13 | \subsection{Raise / Handle} |
---|
[4260566] | 14 | An exception operation has two main parts: raise and handle. |
---|
[6071efc] | 15 | These terms are sometimes known as throw and catch but this work uses |
---|
[4260566] | 16 | throw/catch as a particular kind of raise/handle. |
---|
[4aba055] | 17 | These are the two parts that the user writes and may |
---|
[e3984a68] | 18 | be the only two pieces of the EHM that have any syntax in a language. |
---|
[4260566] | 19 | |
---|
[4aba055] | 20 | \paragraph{Raise} |
---|
[e3984a68] | 21 | The raise is the starting point for exception handling, |
---|
| 22 | by raising an exception, 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 |
---|
[e3984a68] | 26 | the \code{Python}{raise} statement of Python. In real systems, a raise may |
---|
| 27 | perform some other work (such as memory management) but for the |
---|
[299b8b2] | 28 | purposes of this overview that can be ignored. |
---|
[4260566] | 29 | |
---|
[4aba055] | 30 | \paragraph{Handle} |
---|
[e3984a68] | 31 | The primary purpose of an EHM is to run some user code to handle a raised |
---|
| 32 | exception. This code is given, along with some other information, |
---|
| 33 | in a handler. |
---|
[f6106a6] | 34 | |
---|
| 35 | A handler has three common features: the previously mentioned user code, a |
---|
[e3984a68] | 36 | region of code it guards and an exception label/condition that matches |
---|
| 37 | against the raised exception. |
---|
[4aba055] | 38 | Only raises inside the guarded region and raising exceptions that match the |
---|
[f6106a6] | 39 | label can be handled by a given handler. |
---|
[6071efc] | 40 | If multiple handlers could can handle an exception, |
---|
[e3984a68] | 41 | EHMs define a rule to pick one, such as ``best match" or ``first found". |
---|
[4260566] | 42 | |
---|
[f6106a6] | 43 | The @try@ statements of \Cpp, Java and Python are common examples. All three |
---|
[9cdfa5fb] | 44 | also show another common feature of handlers: they are grouped by the guarded |
---|
[f42a6b8] | 45 | region. |
---|
[f6106a6] | 46 | |
---|
[4aba055] | 47 | \subsection{Propagation} |
---|
[de47a9d] | 48 | After an exception is raised comes what is usually the biggest step for the |
---|
[e3984a68] | 49 | EHM: finding and setting up the handler for execution. |
---|
| 50 | The propagation from raise to |
---|
[f6106a6] | 51 | handler can be broken up into three different tasks: searching for a handler, |
---|
[21f2e92] | 52 | matching against the handler and installing the handler. |
---|
[de47a9d] | 53 | |
---|
[4aba055] | 54 | \paragraph{Searching} |
---|
[f6106a6] | 55 | The EHM begins by searching for handlers that might be used to handle |
---|
[e3984a68] | 56 | the exception. |
---|
| 57 | The search will find handlers that have the raise site in their guarded |
---|
[f6106a6] | 58 | region. |
---|
[4aba055] | 59 | The search includes handlers in the current function, as well as any in |
---|
| 60 | callers on the stack that have the function call in their guarded region. |
---|
[f6106a6] | 61 | |
---|
[4aba055] | 62 | \paragraph{Matching} |
---|
[e3984a68] | 63 | Each handler found is with the raised exception. The exception |
---|
| 64 | label defines a condition that is used with the exception and decides if |
---|
[f6106a6] | 65 | there is a match or not. |
---|
[e3984a68] | 66 | % |
---|
[4aba055] | 67 | In languages where the first match is used, this step is intertwined with |
---|
[e3984a68] | 68 | searching; a match check is performed immediately after the search finds |
---|
| 69 | a handler. |
---|
[4260566] | 70 | |
---|
[4aba055] | 71 | \paragraph{Installing} |
---|
[e3984a68] | 72 | After a handler is chosen, it must be made ready to run. |
---|
[f6106a6] | 73 | The implementation can vary widely to fit with the rest of the |
---|
[de47a9d] | 74 | design of the EHM. The installation step might be trivial or it could be |
---|
[4260566] | 75 | the most expensive step in handling an exception. The latter tends to be the |
---|
| 76 | case when stack unwinding is involved. |
---|
[de47a9d] | 77 | |
---|
[6071efc] | 78 | If a matching handler is not guaranteed to be found, the EHM needs a |
---|
[e3984a68] | 79 | different course of action for this case. |
---|
[4aba055] | 80 | This situation only occurs with unchecked exceptions as checked exceptions |
---|
[f42a6b8] | 81 | (such as in Java) can make the guarantee. |
---|
[e3984a68] | 82 | The unhandled action is usually very general, such as aborting the program. |
---|
[4260566] | 83 | |
---|
[4aba055] | 84 | \paragraph{Hierarchy} |
---|
[f6106a6] | 85 | A common way to organize exceptions is in a hierarchical structure. |
---|
[166b384] | 86 | This pattern comes from object-oriented languages where the |
---|
[4260566] | 87 | exception hierarchy is a natural extension of the object hierarchy. |
---|
| 88 | |
---|
[e3984a68] | 89 | Consider the following exception hierarchy: |
---|
[4706098c] | 90 | \begin{center} |
---|
[6a8208cb] | 91 | \input{exception-hierarchy} |
---|
[4706098c] | 92 | \end{center} |
---|
[4aba055] | 93 | A handler labeled with any given exception can handle exceptions of that |
---|
[4260566] | 94 | type or any child type of that exception. The root of the exception hierarchy |
---|
[f42a6b8] | 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, |
---|
[f42a6b8] | 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 |
---|
[9cdfa5fb] | 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 | |
---|
[4aba055] | 107 | \subsection{Completion} |
---|
[6071efc] | 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 | |
---|
[e3984a68] | 113 | The EHM can return control to many different places, where |
---|
[4aba055] | 114 | the most common are after the handler definition (termination) |
---|
| 115 | and after the raise (resumption). |
---|
[4260566] | 116 | |
---|
[4aba055] | 117 | \subsection{Communication} |
---|
[887fc79] | 118 | For effective exception handling, additional information is often passed |
---|
[4aba055] | 119 | from the raise to the handler and back again. |
---|
[9cdfa5fb] | 120 | So far, only communication of the exception's identity is covered. |
---|
[e3984a68] | 121 | A common communication method for adding information to an exception |
---|
| 122 | is putting fields into the exception instance |
---|
[4aba055] | 123 | and giving the handler access to them. |
---|
[e3984a68] | 124 | % You can either have pointers/references in the exception, or have p/rs to |
---|
| 125 | % the exception when it doesn't have to be copied. |
---|
| 126 | Passing references or pointers allows data at the raise location to be |
---|
| 127 | updated, passing information in both directions. |
---|
[4260566] | 128 | |
---|
| 129 | \section{Virtuals} |
---|
[3b8acfb] | 130 | \label{s:virtuals} |
---|
[6aa84e0] | 131 | A common feature in many programming languages is a tool to pair code |
---|
| 132 | (behaviour) with data. |
---|
[166b384] | 133 | In \CFA, this is done with the virtual system, |
---|
[6aa84e0] | 134 | which allow type information to be abstracted away, recovered and allow |
---|
| 135 | operations to be performed on the abstract objects. |
---|
| 136 | |
---|
[f6106a6] | 137 | Virtual types and casts are not part of \CFA's EHM nor are they required for |
---|
[e3984a68] | 138 | an EHM. |
---|
| 139 | However, one of the best ways to support an exception hierarchy |
---|
[4aba055] | 140 | is via a virtual hierarchy and dispatch system. |
---|
[f42a6b8] | 141 | Ideally, the virtual system would have been part of \CFA before the work |
---|
[a6c45c6] | 142 | on exception handling began, but unfortunately it was not. |
---|
[4aba055] | 143 | Hence, only the features and framework needed for the EHM were |
---|
[e3984a68] | 144 | designed and implemented for this thesis. |
---|
| 145 | Other features were considered to ensure that |
---|
[4aba055] | 146 | the structure could accommodate other desirable features in the future |
---|
[e3984a68] | 147 | but are not implemented. |
---|
| 148 | The rest of this section only discusses the implemented subset of the |
---|
[f42a6b8] | 149 | virtual system design. |
---|
[4260566] | 150 | |
---|
| 151 | The virtual system supports multiple ``trees" of types. Each tree is |
---|
| 152 | a simple hierarchy with a single root type. Each type in a tree has exactly |
---|
[f6106a6] | 153 | one parent -- except for the root type which has zero parents -- and any |
---|
[4260566] | 154 | number of children. |
---|
| 155 | Any type that belongs to any of these trees is called a virtual type. |
---|
| 156 | % A type's ancestors are its parent and its parent's ancestors. |
---|
| 157 | % The root type has no ancestors. |
---|
[4aba055] | 158 | % A type's descendants are its children and its children's descendants. |
---|
[4260566] | 159 | |
---|
[9cdfa5fb] | 160 | For the purposes of illustration, a proposed, but unimplemented, syntax |
---|
[13afd0c] | 161 | will be used. Each virtual type is represented by a trait with an annotation |
---|
[e3984a68] | 162 | that makes it a virtual type. This annotation is empty for a root type, which |
---|
| 163 | creates a new tree: |
---|
| 164 | \begin{cfa} |
---|
| 165 | trait root_type(T) virtual() {} |
---|
| 166 | \end{cfa} |
---|
| 167 | The annotation may also refer to any existing virtual type to make this new |
---|
| 168 | type a child of that type and part of the same tree. The parent may itself |
---|
| 169 | be a child or a root type and may have any number of existing children. |
---|
[cd03b76d] | 170 | |
---|
| 171 | % OK, for some reason the b and t positioning options are reversed here. |
---|
| 172 | \begin{minipage}[b]{0.6\textwidth} |
---|
[e3984a68] | 173 | \begin{cfa} |
---|
| 174 | trait child_a(T) virtual(root_type) {} |
---|
| 175 | trait grandchild(T) virtual(child_a) {} |
---|
| 176 | trait child_b(T) virtual(root_type) {} |
---|
| 177 | \end{cfa} |
---|
[cd03b76d] | 178 | \end{minipage} |
---|
| 179 | \begin{minipage}{0.4\textwidth} |
---|
| 180 | \begin{center} |
---|
| 181 | \input{virtual-tree} |
---|
| 182 | \end{center} |
---|
| 183 | \end{minipage} |
---|
[4aba055] | 184 | |
---|
[9cdfa5fb] | 185 | Every virtual type also has a list of virtual members and a unique id. |
---|
| 186 | Both are stored in a virtual table. |
---|
[e3984a68] | 187 | Every instance of a virtual type also has a pointer to a virtual table stored |
---|
| 188 | in it, although there is no per-type virtual table as in many other languages. |
---|
[4260566] | 189 | |
---|
[9cdfa5fb] | 190 | The list of virtual members is accumulated from the root type down the tree. |
---|
| 191 | Every virtual type |
---|
[e3984a68] | 192 | inherits the list of virtual members from its parent and may add more |
---|
| 193 | virtual members to the end of the list which are passed on to its children. |
---|
| 194 | Again, using the unimplemented syntax this might look like: |
---|
| 195 | \begin{cfa} |
---|
| 196 | trait root_type(T) virtual() { |
---|
| 197 | const char * to_string(T const & this); |
---|
| 198 | unsigned int size; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | trait child_type(T) virtual(root_type) { |
---|
| 202 | char * irrelevant_function(int, char); |
---|
| 203 | } |
---|
| 204 | \end{cfa} |
---|
| 205 | % Consider adding a diagram, but we might be good with the explanation. |
---|
| 206 | |
---|
[9cdfa5fb] | 207 | As @child_type@ is a child of @root_type@, it has the virtual members of |
---|
[e3984a68] | 208 | @root_type@ (@to_string@ and @size@) as well as the one it declared |
---|
[13afd0c] | 209 | (@irrelevant_function@). |
---|
[e3984a68] | 210 | |
---|
| 211 | It is important to note that these are virtual members, and may contain |
---|
| 212 | arbitrary fields, functions or otherwise. |
---|
| 213 | The names ``size" and ``align" are reserved for the size and alignment of the |
---|
| 214 | virtual type, and are always automatically initialized as such. |
---|
[9cdfa5fb] | 215 | The other special case is uses of the trait's polymorphic argument |
---|
[e3984a68] | 216 | (@T@ in the example), which are always updated to refer to the current |
---|
[9cdfa5fb] | 217 | virtual type. This allows functions that refer to the polymorphic argument |
---|
[e3984a68] | 218 | to act as traditional virtual methods (@to_string@ in the example), as the |
---|
| 219 | object can always be passed to a virtual method in its virtual table. |
---|
[4260566] | 220 | |
---|
[9cdfa5fb] | 221 | Up until this point, the virtual system is similar to ones found in |
---|
| 222 | object-oriented languages, but this is where \CFA diverges. |
---|
[e3984a68] | 223 | Objects encapsulate a single set of methods in each type, |
---|
| 224 | universally across the entire program, |
---|
| 225 | and indeed all programs that use that type definition. |
---|
| 226 | The only way to change any method is to inherit and define a new type with |
---|
| 227 | its own universal implementation. In this sense, |
---|
| 228 | these object-oriented types are ``closed" and cannot be altered. |
---|
| 229 | % Because really they are class oriented. |
---|
| 230 | |
---|
| 231 | In \CFA, types do not encapsulate any code. |
---|
[9cdfa5fb] | 232 | Whether or not a type satisfies any given assertion, and hence any trait, is |
---|
[e3984a68] | 233 | context sensitive. Types can begin to satisfy a trait, stop satisfying it or |
---|
| 234 | satisfy the same trait at any lexical location in the program. |
---|
[9cdfa5fb] | 235 | In this sense, a type's implementation in the set of functions and variables |
---|
[e3984a68] | 236 | that allow it to satisfy a trait is ``open" and can change |
---|
| 237 | throughout the program. |
---|
[4aba055] | 238 | This capability means it is impossible to pick a single set of functions |
---|
[e3984a68] | 239 | that represent a type's implementation across a program. |
---|
[f6106a6] | 240 | |
---|
| 241 | \CFA side-steps this issue by not having a single virtual table for each |
---|
[4aba055] | 242 | type. A user can define virtual tables that are filled in at their |
---|
| 243 | declaration and given a name. Anywhere that name is visible, even if it is |
---|
[e3984a68] | 244 | defined locally inside a function (although in this case the user must ensure |
---|
| 245 | it outlives any objects that use it), it can be used. |
---|
[4aba055] | 246 | Specifically, a virtual type is ``bound" to a virtual table that |
---|
[08e75215] | 247 | sets the virtual members for that object. The virtual members can be accessed |
---|
| 248 | through the object. |
---|
[4706098c] | 249 | |
---|
[ed4d7c1] | 250 | This means virtual tables are declared and named in \CFA. |
---|
| 251 | They are declared as variables, using the type |
---|
| 252 | @vtable(VIRTUAL_TYPE)@ and any valid name. For example: |
---|
| 253 | \begin{cfa} |
---|
| 254 | vtable(virtual_type_name) table_name; |
---|
| 255 | \end{cfa} |
---|
| 256 | |
---|
[9cdfa5fb] | 257 | Like any variable, they may be forward declared with the @extern@ keyword. |
---|
[ed4d7c1] | 258 | Forward declaring virtual tables is relatively common. |
---|
| 259 | Many virtual types have an ``obvious" implementation that works in most |
---|
| 260 | cases. |
---|
| 261 | A pattern that has appeared in the early work using virtuals is to |
---|
| 262 | implement a virtual table with the the obvious definition and place a forward |
---|
| 263 | declaration of it in the header beside the definition of the virtual type. |
---|
| 264 | |
---|
| 265 | Even on the full declaration, no initializer should be used. |
---|
| 266 | Initialization is automatic. |
---|
| 267 | The type id and special virtual members ``size" and ``align" only depend on |
---|
[9cdfa5fb] | 268 | the virtual type, which is fixed given the type of the virtual table, and |
---|
[ed4d7c1] | 269 | so the compiler fills in a fixed value. |
---|
[9cdfa5fb] | 270 | The other virtual members are resolved using the best match to the member's |
---|
[ed4d7c1] | 271 | name and type, in the same context as the virtual table is declared using |
---|
| 272 | \CFA's normal resolution rules. |
---|
| 273 | |
---|
[9cdfa5fb] | 274 | While much of the virtual infrastructure has been created, |
---|
| 275 | it is currently only used |
---|
[4706098c] | 276 | internally for exception handling. The only user-level feature is the virtual |
---|
[21f2e92] | 277 | cast, which is the same as the \Cpp \code{C++}{dynamic_cast}. |
---|
[7eb6eb5] | 278 | \label{p:VirtualCast} |
---|
[4706098c] | 279 | \begin{cfa} |
---|
[4a36b344] | 280 | (virtual TYPE)EXPRESSION |
---|
[4706098c] | 281 | \end{cfa} |
---|
[29c9b23] | 282 | Note, the syntax and semantics matches a C-cast, rather than the function-like |
---|
| 283 | \Cpp syntax for special casts. Both the type of @EXPRESSION@ and @TYPE@ must be |
---|
[9cdfa5fb] | 284 | pointers to virtual types. |
---|
[de47a9d] | 285 | The cast dynamically checks if the @EXPRESSION@ type is the same or a sub-type |
---|
[29c9b23] | 286 | of @TYPE@, and if true, returns a pointer to the |
---|
[4706098c] | 287 | @EXPRESSION@ object, otherwise it returns @0p@ (null pointer). |
---|
[9cdfa5fb] | 288 | This allows the expression to be used as both a cast and a type check. |
---|
[4706098c] | 289 | |
---|
[ed4d7c1] | 290 | \section{Exceptions} |
---|
| 291 | |
---|
| 292 | The syntax for declaring an exception is the same as declaring a structure |
---|
[9cdfa5fb] | 293 | except the keyword: |
---|
[ed4d7c1] | 294 | \begin{cfa} |
---|
| 295 | exception TYPE_NAME { |
---|
| 296 | FIELDS |
---|
| 297 | }; |
---|
| 298 | \end{cfa} |
---|
| 299 | |
---|
[9cdfa5fb] | 300 | Fields are filled in the same way as a structure as well. However, an extra |
---|
[13afd0c] | 301 | field is added that contains the pointer to the virtual table. |
---|
| 302 | It must be explicitly initialized by the user when the exception is |
---|
[ed4d7c1] | 303 | constructed. |
---|
| 304 | |
---|
| 305 | Here is an example of declaring an exception type along with a virtual table, |
---|
| 306 | assuming the exception has an ``obvious" implementation and a default |
---|
| 307 | virtual table makes sense. |
---|
| 308 | |
---|
| 309 | \begin{minipage}[t]{0.4\textwidth} |
---|
[9cdfa5fb] | 310 | Header (.hfa): |
---|
[ed4d7c1] | 311 | \begin{cfa} |
---|
| 312 | exception Example { |
---|
| 313 | int data; |
---|
| 314 | }; |
---|
| 315 | |
---|
| 316 | extern vtable(Example) |
---|
| 317 | example_base_vtable; |
---|
| 318 | \end{cfa} |
---|
| 319 | \end{minipage} |
---|
| 320 | \begin{minipage}[t]{0.6\textwidth} |
---|
[9cdfa5fb] | 321 | Implementation (.cfa): |
---|
[ed4d7c1] | 322 | \begin{cfa} |
---|
| 323 | vtable(Example) example_base_vtable |
---|
| 324 | \end{cfa} |
---|
| 325 | \vfil |
---|
| 326 | \end{minipage} |
---|
| 327 | |
---|
| 328 | %\subsection{Exception Details} |
---|
[13afd0c] | 329 | This is the only interface needed when raising and handling exceptions. |
---|
[9cdfa5fb] | 330 | However, it is actually a shorthand for a more complex |
---|
| 331 | trait-based interface. |
---|
[4a36b344] | 332 | |
---|
[13afd0c] | 333 | The language views exceptions through a series of traits. |
---|
| 334 | If a type satisfies them, then it can be used as an exception. The following |
---|
[4706098c] | 335 | is the base trait all exceptions need to match. |
---|
| 336 | \begin{cfa} |
---|
| 337 | trait is_exception(exceptT &, virtualT &) { |
---|
[a6c45c6] | 338 | // Numerous imaginary assertions. |
---|
[02b73ea] | 339 | }; |
---|
[4706098c] | 340 | \end{cfa} |
---|
[13afd0c] | 341 | The trait is defined over two types: the exception type and the virtual table |
---|
[4aba055] | 342 | type. Each exception type should have a single virtual table type. |
---|
| 343 | There are no actual assertions in this trait because the trait system |
---|
| 344 | cannot express them yet (adding such assertions would be part of |
---|
[a6c45c6] | 345 | completing the virtual system). The imaginary assertions would probably come |
---|
| 346 | from a trait defined by the virtual system, and state that the exception type |
---|
[9cdfa5fb] | 347 | is a virtual type, |
---|
| 348 | that that the type is a descendant of @exception_t@ (the base exception type) |
---|
[e3984a68] | 349 | and allow the user to find the virtual table type. |
---|
[29c9b23] | 350 | |
---|
| 351 | % I did have a note about how it is the programmer's responsibility to make |
---|
| 352 | % sure the function is implemented correctly. But this is true of every |
---|
[de47a9d] | 353 | % similar system I know of (except Agda's I guess) so I took it out. |
---|
| 354 | |
---|
[f6106a6] | 355 | There are two more traits for exceptions defined as follows: |
---|
[4706098c] | 356 | \begin{cfa} |
---|
[02b73ea] | 357 | trait is_termination_exception( |
---|
[4706098c] | 358 | exceptT &, virtualT & | is_exception(exceptT, virtualT)) { |
---|
[29c9b23] | 359 | void defaultTerminationHandler(exceptT &); |
---|
[02b73ea] | 360 | }; |
---|
| 361 | |
---|
| 362 | trait is_resumption_exception( |
---|
[4706098c] | 363 | exceptT &, virtualT & | is_exception(exceptT, virtualT)) { |
---|
[29c9b23] | 364 | void defaultResumptionHandler(exceptT &); |
---|
[02b73ea] | 365 | }; |
---|
[4706098c] | 366 | \end{cfa} |
---|
[9cdfa5fb] | 367 | Both traits ensure a pair of types is an exception type and |
---|
| 368 | its virtual table type, |
---|
[f6106a6] | 369 | and defines one of the two default handlers. The default handlers are used |
---|
[9cdfa5fb] | 370 | as fallbacks and are discussed in detail in \autoref{s:ExceptionHandling}. |
---|
[de47a9d] | 371 | |
---|
[f6106a6] | 372 | However, all three of these traits can be tricky to use directly. |
---|
| 373 | While there is a bit of repetition required, |
---|
[de47a9d] | 374 | the largest issue is that the virtual table type is mangled and not in a user |
---|
[9cdfa5fb] | 375 | facing way. So, these three macros are provided to wrap these traits to |
---|
[f6106a6] | 376 | simplify referring to the names: |
---|
[f42a6b8] | 377 | @IS_EXCEPTION@, @IS_TERMINATION_EXCEPTION@ and @IS_RESUMPTION_EXCEPTION@. |
---|
[1830a86] | 378 | |
---|
[f6106a6] | 379 | All three take one or two arguments. The first argument is the name of the |
---|
| 380 | exception type. The macro passes its unmangled and mangled form to the trait. |
---|
[1830a86] | 381 | The second (optional) argument is a parenthesized list of polymorphic |
---|
[f6106a6] | 382 | arguments. This argument is only used with polymorphic exceptions and the |
---|
[9cdfa5fb] | 383 | list is passed to both types. |
---|
[f6106a6] | 384 | In the current set-up, the two types always have the same polymorphic |
---|
[9cdfa5fb] | 385 | arguments, so these macros can be used without losing flexibility. |
---|
[29c9b23] | 386 | |
---|
[9cdfa5fb] | 387 | For example, consider a function that is polymorphic over types that have a |
---|
[29c9b23] | 388 | defined arithmetic exception: |
---|
| 389 | \begin{cfa} |
---|
[de47a9d] | 390 | forall(Num | IS_EXCEPTION(Arithmetic, (Num))) |
---|
[29c9b23] | 391 | void some_math_function(Num & left, Num & right); |
---|
| 392 | \end{cfa} |
---|
[4706098c] | 393 | |
---|
[1830a86] | 394 | \section{Exception Handling} |
---|
[f6106a6] | 395 | \label{s:ExceptionHandling} |
---|
[4aba055] | 396 | As stated, |
---|
[21f2e92] | 397 | \CFA provides two kinds of exception handling: termination and resumption. |
---|
[f6106a6] | 398 | These twin operations are the core of \CFA's exception handling mechanism. |
---|
[e3984a68] | 399 | This section covers the general patterns shared by the two operations and |
---|
[9cdfa5fb] | 400 | then goes on to cover the details of each individual operation. |
---|
[de47a9d] | 401 | |
---|
[f6106a6] | 402 | Both operations follow the same set of steps. |
---|
[e3984a68] | 403 | First, a user raises an exception. |
---|
| 404 | Second, the exception propagates up the stack, searching for a handler. |
---|
| 405 | Third, if a handler is found, the exception is caught and the handler is run. |
---|
[4aba055] | 406 | After that control continues at a raise-dependent location. |
---|
[e3984a68] | 407 | As an alternate to the third step, |
---|
| 408 | if a handler is not found, a default handler is run and, if it returns, |
---|
| 409 | then control |
---|
[4aba055] | 410 | continues after the raise. |
---|
[f6106a6] | 411 | |
---|
[e3984a68] | 412 | The differences between the two operations include how propagation is |
---|
[13afd0c] | 413 | performed, where execution continues after an exception is handled |
---|
[e3984a68] | 414 | and which default handler is run. |
---|
[1830a86] | 415 | |
---|
[4706098c] | 416 | \subsection{Termination} |
---|
| 417 | \label{s:Termination} |
---|
[e3984a68] | 418 | Termination handling is the familiar kind of handling |
---|
[9cdfa5fb] | 419 | used in most programming |
---|
[1830a86] | 420 | languages with exception handling. |
---|
[4aba055] | 421 | It is a dynamic, non-local goto. If the raised exception is matched and |
---|
| 422 | handled, the stack is unwound and control (usually) continues in the function |
---|
[f6106a6] | 423 | on the call stack that defined the handler. |
---|
| 424 | Termination is commonly used when an error has occurred and recovery is |
---|
| 425 | impossible locally. |
---|
[1830a86] | 426 | |
---|
| 427 | % (usually) Control can continue in the current function but then a different |
---|
| 428 | % control flow construct should be used. |
---|
[4706098c] | 429 | |
---|
[f6106a6] | 430 | A termination raise is started with the @throw@ statement: |
---|
[4706098c] | 431 | \begin{cfa} |
---|
[4a36b344] | 432 | throw EXPRESSION; |
---|
[4706098c] | 433 | \end{cfa} |
---|
[29c9b23] | 434 | The expression must return a reference to a termination exception, where the |
---|
[f6106a6] | 435 | termination exception is any type that satisfies the trait |
---|
| 436 | @is_termination_exception@ at the call site. |
---|
[4aba055] | 437 | Through \CFA's trait system, the trait functions are implicitly passed into the |
---|
[e3984a68] | 438 | throw code for use by the EHM. |
---|
[f6106a6] | 439 | A new @defaultTerminationHandler@ can be defined in any scope to |
---|
[e3984a68] | 440 | change the throw's behaviour when a handler is not found (see below). |
---|
[de47a9d] | 441 | |
---|
[4aba055] | 442 | The throw copies the provided exception into managed memory to ensure |
---|
[21f2e92] | 443 | the exception is not destroyed if the stack is unwound. |
---|
[f6106a6] | 444 | It is the user's responsibility to ensure the original exception is cleaned |
---|
[4aba055] | 445 | up whether the stack is unwound or not. Allocating it on the stack is |
---|
[f6106a6] | 446 | usually sufficient. |
---|
[de47a9d] | 447 | |
---|
[4aba055] | 448 | % How to say propagation starts, its first sub-step is the search. |
---|
| 449 | Then propagation starts with the search. \CFA uses a ``first match" rule so |
---|
[e3984a68] | 450 | matching is performed with the copied exception as the search key. |
---|
| 451 | It starts from the raise site and proceeds towards base of the stack, |
---|
[1830a86] | 452 | from callee to caller. |
---|
[e3984a68] | 453 | At each stack frame, a check is made for termination handlers defined by the |
---|
[1830a86] | 454 | @catch@ clauses of a @try@ statement. |
---|
[4706098c] | 455 | \begin{cfa} |
---|
[4a36b344] | 456 | try { |
---|
[4706098c] | 457 | GUARDED_BLOCK |
---|
[f6106a6] | 458 | } catch (EXCEPTION_TYPE$\(_1\)$ * [NAME$\(_1\)$]) { |
---|
[4706098c] | 459 | HANDLER_BLOCK$\(_1\)$ |
---|
[f6106a6] | 460 | } catch (EXCEPTION_TYPE$\(_2\)$ * [NAME$\(_2\)$]) { |
---|
[4706098c] | 461 | HANDLER_BLOCK$\(_2\)$ |
---|
[4a36b344] | 462 | } |
---|
[4706098c] | 463 | \end{cfa} |
---|
[4aba055] | 464 | When viewed on its own, a try statement simply executes the statements |
---|
[e3984a68] | 465 | in the \snake{GUARDED_BLOCK} and when those are finished, |
---|
[4aba055] | 466 | the try statement finishes. |
---|
[de47a9d] | 467 | |
---|
| 468 | However, while the guarded statements are being executed, including any |
---|
[4aba055] | 469 | invoked functions, all the handlers in these statements are included in the |
---|
| 470 | search path. |
---|
[e3984a68] | 471 | Hence, if a termination exception is raised, these handlers may be matched |
---|
[4aba055] | 472 | against the exception and may handle it. |
---|
[f6106a6] | 473 | |
---|
| 474 | Exception matching checks the handler in each catch clause in the order |
---|
[4aba055] | 475 | they appear, top to bottom. If the representation of the raised exception type |
---|
[e3984a68] | 476 | is the same or a descendant of @EXCEPTION_TYPE@$_i$, then @NAME@$_i$ |
---|
[21f2e92] | 477 | (if provided) is |
---|
| 478 | bound to a pointer to the exception and the statements in @HANDLER_BLOCK@$_i$ |
---|
| 479 | are executed. If control reaches the end of the handler, the exception is |
---|
[de47a9d] | 480 | freed and control continues after the try statement. |
---|
[4706098c] | 481 | |
---|
[e3984a68] | 482 | If no termination handler is found during the search, then the default handler |
---|
| 483 | (\defaultTerminationHandler) visible at the raise statement is called. |
---|
| 484 | Through \CFA's trait system the best match at the raise statement is used. |
---|
[4aba055] | 485 | This function is run and is passed the copied exception. |
---|
[e3984a68] | 486 | If the default handler finishes, control continues after the raise statement. |
---|
[1830a86] | 487 | |
---|
[f6106a6] | 488 | There is a global @defaultTerminationHandler@ that is polymorphic over all |
---|
[4aba055] | 489 | termination exception types. |
---|
[f6106a6] | 490 | The global default termination handler performs a cancellation |
---|
[e3984a68] | 491 | (as described in \vref{s:Cancellation}) |
---|
| 492 | on the current stack with the copied exception. |
---|
| 493 | Since it is so general, a more specific handler can be defined, |
---|
| 494 | overriding the default behaviour for the specific exception types. |
---|
[6aa84e0] | 495 | |
---|
| 496 | For example, consider an error reading a configuration file. |
---|
[9411cf0] | 497 | This is most likely a problem with the configuration file (@config_error@), |
---|
| 498 | but the function could have been passed the wrong file name (@arg_error@). |
---|
[6aa84e0] | 499 | In this case the function could raise one exception and then, if it is |
---|
| 500 | unhandled, raise the other. |
---|
| 501 | This is not usual behaviour for either exception so changing the |
---|
| 502 | default handler will be done locally: |
---|
| 503 | \begin{cfa} |
---|
| 504 | { |
---|
| 505 | void defaultTerminationHandler(config_error &) { |
---|
| 506 | throw (arg_error){arg_vt}; |
---|
| 507 | } |
---|
| 508 | throw (config_error){config_vt}; |
---|
| 509 | } |
---|
| 510 | \end{cfa} |
---|
[4706098c] | 511 | |
---|
| 512 | \subsection{Resumption} |
---|
| 513 | \label{s:Resumption} |
---|
| 514 | |
---|
[e3984a68] | 515 | Resumption exception handling is less familar form of exception handling, |
---|
| 516 | but is |
---|
[f6106a6] | 517 | just as old~\cite{Goodenough75} and is simpler in many ways. |
---|
| 518 | It is a dynamic, non-local function call. If the raised exception is |
---|
[e3984a68] | 519 | matched, a closure is taken from up the stack and executed, |
---|
[4aba055] | 520 | after which the raising function continues executing. |
---|
| 521 | The common uses for resumption exceptions include |
---|
| 522 | potentially repairable errors, where execution can continue in the same |
---|
| 523 | function once the error is corrected, and |
---|
| 524 | ignorable events, such as logging where nothing needs to happen and control |
---|
[e3984a68] | 525 | should always continue from the raise site. |
---|
| 526 | |
---|
| 527 | Except for the changes to fit into that pattern, resumption exception |
---|
| 528 | handling is symmetric with termination exception handling, by design |
---|
| 529 | (see \autoref{s:Termination}). |
---|
[8483c39a] | 530 | |
---|
[4706098c] | 531 | A resumption raise is started with the @throwResume@ statement: |
---|
| 532 | \begin{cfa} |
---|
[4a36b344] | 533 | throwResume EXPRESSION; |
---|
[4706098c] | 534 | \end{cfa} |
---|
[cd03b76d] | 535 | % The new keywords are currently ``experimental" and not used in this work. |
---|
[e3984a68] | 536 | It works much the same way as the termination raise, except the |
---|
| 537 | type must satisfy the \snake{is_resumption_exception} that uses the |
---|
| 538 | default handler: \defaultResumptionHandler. |
---|
| 539 | This can be specialized for particular exception types. |
---|
| 540 | |
---|
| 541 | At run-time, no exception copy is made. Since |
---|
| 542 | resumption does not unwind the stack nor otherwise remove values from the |
---|
| 543 | current scope, there is no need to manage memory to keep the exception |
---|
| 544 | allocated. |
---|
| 545 | |
---|
| 546 | Then propagation starts with the search, |
---|
| 547 | following the same search path as termination, |
---|
| 548 | from the raise site to the base of stack and top of try statement to bottom. |
---|
| 549 | However, the handlers on try statements are defined by @catchResume@ clauses. |
---|
[4706098c] | 550 | \begin{cfa} |
---|
[4a36b344] | 551 | try { |
---|
[4706098c] | 552 | GUARDED_BLOCK |
---|
[f6106a6] | 553 | } catchResume (EXCEPTION_TYPE$\(_1\)$ * [NAME$\(_1\)$]) { |
---|
[4706098c] | 554 | HANDLER_BLOCK$\(_1\)$ |
---|
[f6106a6] | 555 | } catchResume (EXCEPTION_TYPE$\(_2\)$ * [NAME$\(_2\)$]) { |
---|
[4706098c] | 556 | HANDLER_BLOCK$\(_2\)$ |
---|
[4a36b344] | 557 | } |
---|
[4706098c] | 558 | \end{cfa} |
---|
[f42a6b8] | 559 | Note that termination handlers and resumption handlers may be used together |
---|
[f6106a6] | 560 | in a single try statement, intermixing @catch@ and @catchResume@ freely. |
---|
[4aba055] | 561 | Each type of handler only interacts with exceptions from the matching |
---|
| 562 | kind of raise. |
---|
[e3984a68] | 563 | Like @catch@ clauses, @catchResume@ clauses have no effect if an exception |
---|
| 564 | is not raised. |
---|
[f42a6b8] | 565 | |
---|
[e3984a68] | 566 | The matching rules are exactly the same as well. |
---|
| 567 | The first major difference here is that after |
---|
| 568 | @EXCEPTION_TYPE@$_i$ is matched and @NAME@$_i$ is bound to the exception, |
---|
| 569 | @HANDLER_BLOCK@$_i$ is executed right away without first unwinding the stack. |
---|
[9cdfa5fb] | 570 | After the block has finished running, control jumps to the raise site, where |
---|
[e3984a68] | 571 | the just handled exception came from, and continues executing after it, |
---|
| 572 | not after the try statement. |
---|
[6aa84e0] | 573 | |
---|
| 574 | For instance, a resumption used to send messages to the logger may not |
---|
| 575 | need to be handled at all. Putting the following default handler |
---|
[9411cf0] | 576 | at the global scope can make handling that exception optional by default. |
---|
[6aa84e0] | 577 | \begin{cfa} |
---|
| 578 | void defaultResumptionHandler(log_message &) { |
---|
| 579 | // Nothing, it is fine not to handle logging. |
---|
| 580 | } |
---|
| 581 | // ... No change at raise sites. ... |
---|
| 582 | throwResume (log_message){strlit_log, "Begin event processing."} |
---|
| 583 | \end{cfa} |
---|
[1830a86] | 584 | |
---|
[f6106a6] | 585 | \subsubsection{Resumption Marking} |
---|
[df24d37] | 586 | \label{s:ResumptionMarking} |
---|
[1830a86] | 587 | A key difference between resumption and termination is that resumption does |
---|
[e3984a68] | 588 | not unwind the stack. A side effect is that, when a handler is matched |
---|
| 589 | and run, its try block (the guarded statements) and every try statement |
---|
[9cdfa5fb] | 590 | searched before it are still on the stack. Their presence can lead to |
---|
[cd03b76d] | 591 | the recursive resumption problem.\cite{Buhr00a} |
---|
| 592 | % Other possible citation is MacLaren77, but the form is different. |
---|
[1830a86] | 593 | |
---|
| 594 | The recursive resumption problem is any situation where a resumption handler |
---|
| 595 | ends up being called while it is running. |
---|
| 596 | Consider a trivial case: |
---|
| 597 | \begin{cfa} |
---|
| 598 | try { |
---|
| 599 | throwResume (E &){}; |
---|
| 600 | } catchResume(E *) { |
---|
| 601 | throwResume (E &){}; |
---|
| 602 | } |
---|
| 603 | \end{cfa} |
---|
[4aba055] | 604 | When this code is executed, the guarded @throwResume@ starts a |
---|
| 605 | search and matches the handler in the @catchResume@ clause. This |
---|
[e3984a68] | 606 | call is placed on the stack above the try-block. |
---|
| 607 | Now the second raise in the handler searches the same try block, |
---|
| 608 | matches again and then puts another instance of the |
---|
[4aba055] | 609 | same handler on the stack leading to infinite recursion. |
---|
[1830a86] | 610 | |
---|
[f42a6b8] | 611 | While this situation is trivial and easy to avoid, much more complex cycles |
---|
| 612 | can form with multiple handlers and different exception types. |
---|
[e3984a68] | 613 | To prevent all of these cases, each try statement is ``marked" from the |
---|
| 614 | time the exception search reaches it to either when a handler completes |
---|
| 615 | handling that exception or when the search reaches the base |
---|
[4aba055] | 616 | of the stack. |
---|
| 617 | While a try statement is marked, its handlers are never matched, effectively |
---|
[21f2e92] | 618 | skipping over it to the next try statement. |
---|
[4a36b344] | 619 | |
---|
[6a8208cb] | 620 | \begin{center} |
---|
| 621 | \input{stack-marking} |
---|
| 622 | \end{center} |
---|
[de47a9d] | 623 | |
---|
[9cdfa5fb] | 624 | There are other sets of marking rules that could be used. |
---|
| 625 | For instance, marking just the handlers that caught the exception |
---|
[4aba055] | 626 | would also prevent recursive resumption. |
---|
[9cdfa5fb] | 627 | However, the rules selected mirror what happens with termination, |
---|
[e3984a68] | 628 | so this reduces the amount of rules and patterns a programmer has to know. |
---|
[4706098c] | 629 | |
---|
[e3984a68] | 630 | The marked try statements are the ones that would be removed from |
---|
| 631 | the stack for a termination exception, \ie those on the stack |
---|
[4aba055] | 632 | between the handler and the raise statement. |
---|
| 633 | This symmetry applies to the default handler as well, as both kinds of |
---|
| 634 | default handlers are run at the raise statement, rather than (physically |
---|
| 635 | or logically) at the bottom of the stack. |
---|
| 636 | % In early development having the default handler happen after |
---|
| 637 | % unmarking was just more useful. We assume that will continue. |
---|
[4706098c] | 638 | |
---|
| 639 | \section{Conditional Catch} |
---|
[de47a9d] | 640 | Both termination and resumption handler clauses can be given an additional |
---|
| 641 | condition to further control which exceptions they handle: |
---|
[4706098c] | 642 | \begin{cfa} |
---|
[f6106a6] | 643 | catch (EXCEPTION_TYPE * [NAME] ; CONDITION) |
---|
[4706098c] | 644 | \end{cfa} |
---|
| 645 | First, the same semantics is used to match the exception type. Second, if the |
---|
| 646 | exception matches, @CONDITION@ is executed. The condition expression may |
---|
[de47a9d] | 647 | reference all names in scope at the beginning of the try block and @NAME@ |
---|
[1c1c180] | 648 | introduced in the handler clause. If the condition is true, then the handler |
---|
[1830a86] | 649 | matches. Otherwise, the exception search continues as if the exception type |
---|
| 650 | did not match. |
---|
[f6106a6] | 651 | |
---|
[4aba055] | 652 | The condition matching allows finer matching by checking |
---|
[f6106a6] | 653 | more kinds of information than just the exception type. |
---|
[4706098c] | 654 | \begin{cfa} |
---|
| 655 | try { |
---|
[f6106a6] | 656 | handle1 = open( f1, ... ); |
---|
| 657 | handle2 = open( f2, ... ); |
---|
| 658 | handle3 = open( f3, ... ); |
---|
[4706098c] | 659 | ... |
---|
[de47a9d] | 660 | } catch( IOFailure * f ; fd( f ) == f1 ) { |
---|
[f6106a6] | 661 | // Only handle IO failure for f1. |
---|
| 662 | } catch( IOFailure * f ; fd( f ) == f3 ) { |
---|
| 663 | // Only handle IO failure for f3. |
---|
[4706098c] | 664 | } |
---|
[e3984a68] | 665 | // Handle a failure relating to f2 further down the stack. |
---|
[4706098c] | 666 | \end{cfa} |
---|
[9cdfa5fb] | 667 | In this example, the file that experienced the IO error is used to decide |
---|
[f6106a6] | 668 | which handler should be run, if any at all. |
---|
| 669 | |
---|
| 670 | \begin{comment} |
---|
| 671 | % I know I actually haven't got rid of them yet, but I'm going to try |
---|
| 672 | % to write it as if I had and see if that makes sense: |
---|
| 673 | \section{Reraising} |
---|
| 674 | \label{s:Reraising} |
---|
[4706098c] | 675 | Within the handler block or functions called from the handler block, it is |
---|
| 676 | possible to reraise the most recently caught exception with @throw@ or |
---|
[1830a86] | 677 | @throwResume@, respectively. |
---|
[4706098c] | 678 | \begin{cfa} |
---|
[29c9b23] | 679 | try { |
---|
| 680 | ... |
---|
| 681 | } catch( ... ) { |
---|
[1830a86] | 682 | ... throw; |
---|
[4706098c] | 683 | } catchResume( ... ) { |
---|
[1830a86] | 684 | ... throwResume; |
---|
[4706098c] | 685 | } |
---|
| 686 | \end{cfa} |
---|
| 687 | The only difference between a raise and a reraise is that reraise does not |
---|
| 688 | create a new exception; instead it continues using the current exception, \ie |
---|
| 689 | no allocation and copy. However the default handler is still set to the one |
---|
| 690 | visible at the raise point, and hence, for termination could refer to data that |
---|
| 691 | is part of an unwound stack frame. To prevent this problem, a new default |
---|
| 692 | handler is generated that does a program-level abort. |
---|
[f6106a6] | 693 | \end{comment} |
---|
| 694 | |
---|
| 695 | \subsection{Comparison with Reraising} |
---|
[9cdfa5fb] | 696 | In languages without conditional catch -- that is, no ability to match an |
---|
| 697 | exception based on something other than its type -- it can be mimicked |
---|
[e3984a68] | 698 | by matching all exceptions of the right type, checking any additional |
---|
| 699 | conditions inside the handler and re-raising the exception if it does not |
---|
| 700 | match those. |
---|
| 701 | |
---|
| 702 | Here is a minimal example comparing both patterns, using @throw;@ |
---|
[9cdfa5fb] | 703 | (no operand) to start a re-raise. |
---|
[e3984a68] | 704 | \begin{center} |
---|
| 705 | \begin{tabular}{l r} |
---|
[f6106a6] | 706 | \begin{cfa} |
---|
| 707 | try { |
---|
[f42a6b8] | 708 | do_work_may_throw(); |
---|
[e3984a68] | 709 | } catch(exception_t * exc ; |
---|
| 710 | can_handle(exc)) { |
---|
[f42a6b8] | 711 | handle(exc); |
---|
[f6106a6] | 712 | } |
---|
| 713 | |
---|
[e3984a68] | 714 | |
---|
| 715 | |
---|
| 716 | \end{cfa} |
---|
| 717 | & |
---|
[f6106a6] | 718 | \begin{cfa} |
---|
| 719 | try { |
---|
[f42a6b8] | 720 | do_work_may_throw(); |
---|
[e3984a68] | 721 | } catch(exception_t * exc) { |
---|
[f42a6b8] | 722 | if (can_handle(exc)) { |
---|
| 723 | handle(exc); |
---|
| 724 | } else { |
---|
| 725 | throw; |
---|
| 726 | } |
---|
[f6106a6] | 727 | } |
---|
| 728 | \end{cfa} |
---|
[e3984a68] | 729 | \end{tabular} |
---|
| 730 | \end{center} |
---|
[9cdfa5fb] | 731 | At first glance, catch-and-reraise may appear to just be a quality-of-life |
---|
[e3984a68] | 732 | feature, but there are some significant differences between the two |
---|
[9cdfa5fb] | 733 | strategies. |
---|
[e3984a68] | 734 | |
---|
| 735 | A simple difference that is more important for \CFA than many other languages |
---|
[9cdfa5fb] | 736 | is that the raise site changes with a re-raise, but does not with a |
---|
[e3984a68] | 737 | conditional catch. |
---|
| 738 | This is important in \CFA because control returns to the raise site to run |
---|
[9cdfa5fb] | 739 | the per-site default handler. Because of this, only a conditional catch can |
---|
[e3984a68] | 740 | allow the original raise to continue. |
---|
| 741 | |
---|
| 742 | The more complex issue comes from the difference in how conditional |
---|
| 743 | catches and re-raises handle multiple handlers attached to a single try |
---|
| 744 | statement. A conditional catch will continue checking later handlers while |
---|
| 745 | a re-raise will skip them. |
---|
| 746 | If the different handlers could handle some of the same exceptions, |
---|
| 747 | translating a try statement that uses one to use the other can quickly |
---|
| 748 | become non-trivial: |
---|
| 749 | |
---|
| 750 | \noindent |
---|
| 751 | Original, with conditional catch: |
---|
| 752 | \begin{cfa} |
---|
| 753 | ... |
---|
| 754 | } catch (an_exception * e ; check_a(e)) { |
---|
| 755 | handle_a(e); |
---|
| 756 | } catch (exception_t * e ; check_b(e)) { |
---|
| 757 | handle_b(e); |
---|
| 758 | } |
---|
| 759 | \end{cfa} |
---|
| 760 | Translated, with re-raise: |
---|
| 761 | \begin{cfa} |
---|
| 762 | ... |
---|
| 763 | } catch (exception_t * e) { |
---|
| 764 | an_exception * an_e = (virtual an_exception *)e; |
---|
| 765 | if (an_e && check_a(an_e)) { |
---|
| 766 | handle_a(an_e); |
---|
| 767 | } else if (check_b(e)) { |
---|
| 768 | handle_b(e); |
---|
| 769 | } else { |
---|
| 770 | throw; |
---|
| 771 | } |
---|
| 772 | } |
---|
| 773 | \end{cfa} |
---|
| 774 | (There is a simpler solution if @handle_a@ never raises exceptions, |
---|
| 775 | using nested try statements.) |
---|
| 776 | |
---|
| 777 | % } catch (an_exception * e ; check_a(e)) { |
---|
| 778 | % handle_a(e); |
---|
| 779 | % } catch (exception_t * e ; !(virtual an_exception *)e && check_b(e)) { |
---|
| 780 | % handle_b(e); |
---|
| 781 | % } |
---|
[4aba055] | 782 | % |
---|
[e3984a68] | 783 | % } catch (an_exception * e) |
---|
| 784 | % if (check_a(e)) { |
---|
| 785 | % handle_a(e); |
---|
| 786 | % } else throw; |
---|
| 787 | % } catch (exception_t * e) |
---|
| 788 | % if (check_b(e)) { |
---|
| 789 | % handle_b(e); |
---|
| 790 | % } else throw; |
---|
| 791 | % } |
---|
[9cdfa5fb] | 792 | In similar simple examples, translating from re-raise to conditional catch |
---|
| 793 | takes less code but it does not have a general, trivial solution either. |
---|
[e3984a68] | 794 | |
---|
| 795 | So, given that the two patterns do not trivially translate into each other, |
---|
| 796 | it becomes a matter of which on should be encouraged and made the default. |
---|
[9cdfa5fb] | 797 | From the premise that if a handler could handle an exception then it |
---|
[e3984a68] | 798 | should, it follows that checking as many handlers as possible is preferred. |
---|
[9cdfa5fb] | 799 | So, conditional catch and checking later handlers is a good default. |
---|
[4a36b344] | 800 | |
---|
| 801 | \section{Finally Clauses} |
---|
[f6106a6] | 802 | \label{s:FinallyClauses} |
---|
[9cdfa5fb] | 803 | Finally clauses are used to perform unconditional cleanup when leaving a |
---|
[f6106a6] | 804 | scope and are placed at the end of a try statement after any handler clauses: |
---|
[4706098c] | 805 | \begin{cfa} |
---|
[4a36b344] | 806 | try { |
---|
[4706098c] | 807 | GUARDED_BLOCK |
---|
[29c9b23] | 808 | } ... // any number or kind of handler clauses |
---|
| 809 | ... finally { |
---|
[4706098c] | 810 | FINALLY_BLOCK |
---|
[4a36b344] | 811 | } |
---|
[4706098c] | 812 | \end{cfa} |
---|
[29c9b23] | 813 | The @FINALLY_BLOCK@ is executed when the try statement is removed from the |
---|
[1830a86] | 814 | stack, including when the @GUARDED_BLOCK@ finishes, any termination handler |
---|
[f42a6b8] | 815 | finishes or during an unwind. |
---|
[29c9b23] | 816 | The only time the block is not executed is if the program is exited before |
---|
[1830a86] | 817 | the stack is unwound. |
---|
[4706098c] | 818 | |
---|
| 819 | Execution of the finally block should always finish, meaning control runs off |
---|
[f6106a6] | 820 | the end of the block. This requirement ensures control always continues as if |
---|
[9cdfa5fb] | 821 | the finally clause is not present, \ie finally is for cleanup, not changing |
---|
[f6106a6] | 822 | control flow. |
---|
| 823 | Because of this requirement, local control flow out of the finally block |
---|
[1c1c180] | 824 | is forbidden. The compiler precludes any @break@, @continue@, @fallthru@ or |
---|
[4706098c] | 825 | @return@ that causes control to leave the finally block. Other ways to leave |
---|
[9cdfa5fb] | 826 | the finally block, such as a @longjmp@ or termination are much harder to check, |
---|
| 827 | and at best require additional run-time overhead, and so are only |
---|
[1830a86] | 828 | discouraged. |
---|
| 829 | |
---|
[9cdfa5fb] | 830 | Not all languages with unwinding have finally clauses. Notably, \Cpp does |
---|
[e3984a68] | 831 | without it as destructors, and the RAII design pattern, serve a similar role. |
---|
| 832 | Although destructors and finally clauses can be used for the same cases, |
---|
[4aba055] | 833 | they have their own strengths, similar to top-level function and lambda |
---|
| 834 | functions with closures. |
---|
[e3984a68] | 835 | Destructors take more work to create, but if there is clean-up code |
---|
| 836 | that needs to be run every time a type is used, they are much easier |
---|
[9cdfa5fb] | 837 | to set up for each use. % It's automatic. |
---|
| 838 | On the other hand, finally clauses capture the local context, so are easy to |
---|
| 839 | use when the cleanup is not dependent on the type of a variable or requires |
---|
[4aba055] | 840 | information from multiple variables. |
---|
[4a36b344] | 841 | |
---|
| 842 | \section{Cancellation} |
---|
[f6106a6] | 843 | \label{s:Cancellation} |
---|
[de47a9d] | 844 | Cancellation is a stack-level abort, which can be thought of as as an |
---|
[f6106a6] | 845 | uncatchable termination. It unwinds the entire current stack, and if |
---|
[9cdfa5fb] | 846 | possible, forwards the cancellation exception to a different stack. |
---|
[4706098c] | 847 | |
---|
[29c9b23] | 848 | Cancellation is not an exception operation like termination or resumption. |
---|
[4706098c] | 849 | There is no special statement for starting a cancellation; instead the standard |
---|
[9cdfa5fb] | 850 | library function @cancel_stack@ is called, passing an exception. Unlike a |
---|
| 851 | raise, this exception is not used in matching, only to pass information about |
---|
[4706098c] | 852 | the cause of the cancellation. |
---|
[e3984a68] | 853 | Finally, as no handler is provided, there is no default handler. |
---|
[4706098c] | 854 | |
---|
[9cdfa5fb] | 855 | After @cancel_stack@ is called, the exception is copied into the EHM's memory |
---|
[4aba055] | 856 | and the current stack is unwound. |
---|
| 857 | The behaviour after that depends on the kind of stack being cancelled. |
---|
[a6c45c6] | 858 | |
---|
| 859 | \paragraph{Main Stack} |
---|
[9cdfa5fb] | 860 | The main stack is the one used by |
---|
| 861 | the program's main function at the start of execution, |
---|
[f6106a6] | 862 | and is the only stack in a sequential program. |
---|
[9cdfa5fb] | 863 | After the main stack is unwound, there is a program-level abort. |
---|
[f6106a6] | 864 | |
---|
[e3984a68] | 865 | The first reason for this behaviour is for sequential programs where there |
---|
[9cdfa5fb] | 866 | is only one stack, and hence no stack to pass information to. |
---|
[e3984a68] | 867 | Second, even in concurrent programs, the main stack has no dependency |
---|
| 868 | on another stack and no reliable way to find another living stack. |
---|
| 869 | Finally, keeping the same behaviour in both sequential and concurrent |
---|
| 870 | programs is simple and easy to understand. |
---|
[4706098c] | 871 | |
---|
[a6c45c6] | 872 | \paragraph{Thread Stack} |
---|
[f6106a6] | 873 | A thread stack is created for a \CFA @thread@ object or object that satisfies |
---|
| 874 | the @is_thread@ trait. |
---|
[4aba055] | 875 | After a thread stack is unwound, the exception is stored until another |
---|
[f6106a6] | 876 | thread attempts to join with it. Then the exception @ThreadCancelled@, |
---|
| 877 | which stores a reference to the thread and to the exception passed to the |
---|
[4aba055] | 878 | cancellation, is reported from the join to the joining thread. |
---|
[f6106a6] | 879 | There is one difference between an explicit join (with the @join@ function) |
---|
| 880 | and an implicit join (from a destructor call). The explicit join takes the |
---|
| 881 | default handler (@defaultResumptionHandler@) from its calling context while |
---|
[9cdfa5fb] | 882 | the implicit join provides its own, which does a program abort if the |
---|
[f6106a6] | 883 | @ThreadCancelled@ exception cannot be handled. |
---|
| 884 | |
---|
[4aba055] | 885 | The communication and synchronization are done here because threads only have |
---|
| 886 | two structural points (not dependent on user-code) where |
---|
| 887 | communication/synchronization happens: start and join. |
---|
[f6106a6] | 888 | Since a thread must be running to perform a cancellation (and cannot be |
---|
| 889 | cancelled from another stack), the cancellation must be after start and |
---|
[4aba055] | 890 | before the join, so join is used. |
---|
[f6106a6] | 891 | |
---|
| 892 | % TODO: Find somewhere to discuss unwind collisions. |
---|
| 893 | The difference between the explicit and implicit join is for safety and |
---|
| 894 | debugging. It helps prevent unwinding collisions by avoiding throwing from |
---|
| 895 | a destructor and prevents cascading the error across multiple threads if |
---|
| 896 | the user is not equipped to deal with it. |
---|
[33e1c91] | 897 | It is always possible to add an explicit join if that is the desired behaviour. |
---|
| 898 | |
---|
| 899 | With explicit join and a default handler that triggers a cancellation, it is |
---|
[e3984a68] | 900 | possible to cascade an error across any number of threads, |
---|
| 901 | alternating between the resumption (possibly termination) and cancellation, |
---|
| 902 | cleaning up each |
---|
[33e1c91] | 903 | in turn, until the error is handled or the main thread is reached. |
---|
[f6106a6] | 904 | |
---|
[a6c45c6] | 905 | \paragraph{Coroutine Stack} |
---|
[f6106a6] | 906 | A coroutine stack is created for a @coroutine@ object or object that |
---|
| 907 | satisfies the @is_coroutine@ trait. |
---|
[4aba055] | 908 | After a coroutine stack is unwound, control returns to the @resume@ function |
---|
| 909 | that most recently resumed it. @resume@ reports a |
---|
[814f87d] | 910 | @CoroutineCancelled@ exception, which contains a reference to the cancelled |
---|
[f6106a6] | 911 | coroutine and the exception used to cancel it. |
---|
[4aba055] | 912 | The @resume@ function also takes the \defaultResumptionHandler{} from the |
---|
[21f2e92] | 913 | caller's context and passes it to the internal report. |
---|
[f6106a6] | 914 | |
---|
[e3984a68] | 915 | A coroutine only knows of two other coroutines, |
---|
| 916 | its starter and its last resumer. |
---|
[4aba055] | 917 | The starter has a much more distant connection, while the last resumer just |
---|
[f6106a6] | 918 | (in terms of coroutine state) called resume on this coroutine, so the message |
---|
| 919 | is passed to the latter. |
---|
[33e1c91] | 920 | |
---|
| 921 | With a default handler that triggers a cancellation, it is possible to |
---|
[e3984a68] | 922 | cascade an error across any number of coroutines, |
---|
| 923 | alternating between the resumption (possibly termination) and cancellation, |
---|
| 924 | cleaning up each in turn, |
---|
[33e1c91] | 925 | until the error is handled or a thread stack is reached. |
---|