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