\chapter{Exception Features} \label{c:features} This chapter covers the design and user interface of the \CFA EHM and begins with a general overview of EHMs. It is not a strict definition of all EHMs nor an exhaustive list of all possible features. However it does cover the most common structure and features found in them. \section{Overview of EHMs} % We should cover what is an exception handling mechanism and what is an % exception before this. Probably in the introduction. Some of this could % move there. \subsection{Raise / Handle} An exception operation has two main parts: raise and handle. These terms are sometimes known as throw and catch but this work uses throw/catch as a particular kind of raise/handle. These are the two parts that the user writes and may be the only two pieces of the EHM that have any syntax in a language. \paragraph{Raise} The raise is the starting point for exception handling by raising an exception, which passes it to the EHM. Some well known examples include the @throw@ statements of \Cpp and Java and the \code{Python}{raise} statement of Python. In real systems, a raise may perform some other work (such as memory management) but for the purposes of this overview that can be ignored. \paragraph{Handle} The primary purpose of an EHM is to run some user code to handle a raised exception. This code is given, with some other information, in a handler. A handler has three common features: the previously mentioned user code, a region of code it guards, and an exception label/condition that matches the raised exception. Only raises inside the guarded region and raising exceptions that match the label can be handled by a given handler. If multiple handlers could can handle an exception, EHMs define a rule to pick one, such as ``best match" or ``first found". The @try@ statements of \Cpp, Java and Python are common examples. All three show the common features of guarded region, raise, matching and handler. \begin{cfa} try { // guarded region ... throw exception; // raise ... } catch( exception ) { // matching condition, with exception label ... // handler code } \end{cfa} \subsection{Propagation} After an exception is raised comes what is usually the biggest step for the EHM: finding and setting up the handler for execution. The propagation from raise to handler can be broken up into three different tasks: searching for a handler, matching against the handler and installing the handler. \paragraph{Searching} The EHM begins by searching for handlers that might be used to handle the exception. The search is restricted to handlers that have the raise site in their guarded region. The search includes handlers in the current function, as well as any in callers on the stack that have the function call in their guarded region. \paragraph{Matching} Each handler found is matched with the raised exception. The exception label defines a condition that is used with the exception and decides if there is a match or not. In languages where the first match is used, this step is intertwined with searching; a match check is performed immediately after the search finds a handler. \paragraph{Installing} After a handler is chosen, it must be made ready to run. The implementation can vary widely to fit with the rest of the design of the EHM. The installation step might be trivial or it could be the most expensive step in handling an exception. The latter tends to be the case when stack unwinding is involved. If a matching handler is not guaranteed to be found, the EHM needs a different course of action for this case. This situation only occurs with unchecked exceptions as checked exceptions (such as in Java) are guaranteed to find a matching handler. The unhandled action is usually very general, such as aborting the program. \paragraph{Hierarchy} A common way to organize exceptions is in a hierarchical structure. This pattern comes from object-orientated languages where the exception hierarchy is a natural extension of the object hierarchy. Consider the following exception hierarchy: \begin{center} \input{exception-hierarchy} \end{center} A handler labeled with any given exception can handle exceptions of that type or any child type of that exception. The root of the exception hierarchy (here \code{C}{exception}) acts as a catch-all, leaf types catch single types, and the exceptions in the middle can be used to catch different groups of related exceptions. This system has some notable advantages, such as multiple levels of grouping, the ability for libraries to add new exception types, and the isolation between different sub-hierarchies. This design is used in \CFA even though it is not a object-orientated language; so different tools are used to create the hierarchy. % Could I cite the rational for the Python IO exception rework? \subsection{Completion} After the handler has finished, the entire exception operation has to complete and continue executing somewhere else. This step is usually simple, both logically and in its implementation, as the installation of the handler is usually set up to do most of the work. The EHM can return control to many different places, where the most common are after the handler definition (termination) and after the raise (resumption). \subsection{Communication} For effective exception handling, additional information is often passed from the raise to the handler and back again. So far, only communication of the exception's identity is covered. A common communication method for passing more information is putting fields into the exception instance and giving the handler access to them. Using reference fields pointing to data at the raise location allows data to be passed in both directions. \section{Virtuals} \label{s:Virtuals} Virtual types and casts are not part of \CFA's EHM nor are they required for an EHM. However, one of the best ways to support an exception hierarchy is via a virtual hierarchy and dispatch system. Ideally, the virtual system should have been part of \CFA before the work on exception handling began, but unfortunately it was not. Hence, only the features and framework needed for the EHM were designed and implemented for this thesis. Other features were considered to ensure that the structure could accommodate other desirable features in the future but are not implemented. The rest of this section only discusses the implemented subset of the virtual-system design. The virtual system supports multiple ``trees" of types. Each tree is a simple hierarchy with a single root type. Each type in a tree has exactly one parent -- except for the root type which has zero parents -- and any number of children. Any type that belongs to any of these trees is called a virtual type. For example, the following hypothetical syntax creates two virtual-type trees. \begin{flushleft} \lstDeleteShortInline@ \begin{tabular}{@{\hspace{20pt}}l@{\hspace{20pt}}l} \begin{cfa} vtype V0, V1(V0), V2(V0); vtype W0, W1(W0), W2(W1); \end{cfa} & \raisebox{-0.6\totalheight}{\input{vtable}} \end{tabular} \lstMakeShortInline@ \end{flushleft} % A type's ancestors are its parent and its parent's ancestors. % The root type has no ancestors. % A type's descendants are its children and its children's descendants. Every virtual type (tree node) has a pointer to a virtual table with a unique @Id@ and a list of virtual members (see \autoref{s:VirtualSystem} for details). Children inherit their parent's list of virtual members but may add and/or replace members. For example, \begin{cfa} vtable W0 | { int ?