Changeset 9411cf0


Ignore:
Timestamp:
Sep 24, 2021, 12:09:38 PM (2 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
master
Children:
f93c50a
Parents:
814f87d
Message:

Andrew MMath: Maybe the last changes to the thesis.

Location:
doc/theses/andrew_beach_MMath
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/andrew_beach_MMath/features.tex

    r814f87d r9411cf0  
    495495
    496496For example, consider an error reading a configuration file.
    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@.
     497This is most likely a problem with the configuration file (@config_error@),
     498but the function could have been passed the wrong file name (@arg_error@).
    499499In this case the function could raise one exception and then, if it is
    500500unhandled, raise the other.
     
    574574For instance, a resumption used to send messages to the logger may not
    575575need to be handled at all. Putting the following default handler
    576 at the global scope can make handling the exception optional by default.
     576at the global scope can make handling that exception optional by default.
    577577\begin{cfa}
    578578void defaultResumptionHandler(log_message &) {
  • doc/theses/andrew_beach_MMath/intro.tex

    r814f87d r9411cf0  
    3939it returns control to that function.
    4040\begin{center}
    41 \input{termination}
    42 
    43 \medskip
     41%\input{termination}
     42%
     43%\medskip
    4444\input{termhandle.pstex_t}
     45% I hate these diagrams, but I can't access xfig to fix them and they are
     46% better than the alternative.
    4547\end{center}
    46 \todo*{Can I make the new diagrams fit the old style?}
    4748
    4849Resumption exception handling searches the stack for a handler and then calls
     
    5354that preformed the raise, usually starting after the raise.
    5455\begin{center}
    55 \input{resumption}
    56 
    57 \medskip
     56%\input{resumption}
     57%
     58%\medskip
    5859\input{resumhandle.pstex_t}
     60% The other one.
    5961\end{center}
    6062
     
    243245through multiple functions before it is addressed.
    244246
     247Here is an example of the pattern in Bash, where commands can only  ``return"
     248numbers and most output is done through streams of text.
     249\begin{lstlisting}[language=bash,escapechar={}]
     250# Immediately after running a command:
     251case $? in
     2520)
     253        # Success
     254        ;;
     2551)
     256        # Error Code 1
     257        ;;
     2582|3)
     259        # Error Code 2 or Error Code 3
     260        ;;
     261# Add more cases as needed.
     262asac
     263\end{lstlisting}
     264
    245265\item\emph{Special Return with Global Store}:
    246266Similar to the error codes pattern but the function itself only returns
     
    252272
    253273This approach avoids the multiple results issue encountered with straight
    254 error codes but otherwise has the same disadvantages and more.
     274error codes as only a single error value has to be returned,
     275but otherwise has the same disadvantages and more.
    255276Every function that reads or writes to the global store must agree on all
    256277possible errors and managing it becomes more complex with concurrency.
     278
     279This example shows some of what has to be done to robustly handle a C
     280standard library function that reports errors this way.
     281\begin{lstlisting}[language=C]
     282// Make sure to clear the store.
     283errno = 0;
     284// Now a library function can set the error.
     285int handle = open(path_name, flags);
     286if (-1 == handle) {
     287        switch (errno) {
     288    case ENAMETOOLONG:
     289                // path_name is a bad argument.
     290                break;
     291        case ENFILE:
     292                // A system resource has been exausted.
     293                break;
     294        // And many more...
     295    }
     296}
     297\end{lstlisting}
     298% cite open man page?
    257299
    258300\item\emph{Return Union}:
     
    277319% Rust's \code{rust}{Result<T, E>}
    278320
     321This is a simple example of examining the result of a failing function in
     322Haskell, using its \code{haskell}{Either} type.
     323Examining \code{haskell}{error} further would likely involve more matching,
     324but the type of \code{haskell}{error} is user defined so there are no
     325general cases.
     326\begin{lstlisting}[language=haskell]
     327case failingFunction argA argB of
     328    Right value -> -- Use the successful computed value.
     329    Left error -> -- Handle the produced error.
     330\end{lstlisting}
     331
    279332Return unions as monads will result in the same code, but can hide most
    280333of the work to propagate errors in simple cases. The code to actually handle
    281334the errors, or to interact with other monads (a common case in these
    282335languages) still has to be written by hand.
     336
     337If \code{haskell}{failingFunction} is implemented with two helpers that
     338use the same error type, then it can be implemented with a \code{haskell}{do}
     339block.
     340\begin{lstlisting}[language=haskell]
     341failingFunction x y = do
     342        z <- helperOne x
     343        helperTwo y z
     344\end{lstlisting}
    283345
    284346\item\emph{Handler Functions}:
     
    301363function calls, but cheaper (constant time) to call,
    302364they are more suited to more frequent (less exceptional) situations.
     365Although, in \Cpp and other languages that do not have checked exceptions,
     366they can actually be enforced by the type system be more reliable.
     367
     368This is a more local example in \Cpp, using a function to provide
     369a default value for a mapping.
     370\begin{lstlisting}[language=C++]
     371ValueT Map::key_or_default(KeyT key, ValueT(*make_default)(KeyT)) {
     372        ValueT * value = find_value(key);
     373        if (nullptr != value) {
     374                return *value;
     375        } else {
     376                return make_default(key);
     377        }
     378}
     379\end{lstlisting}
    303380\end{itemize}
    304381
Note: See TracChangeset for help on using the changeset viewer.