15-150: Principles of Functional Programming

Lecture 12: Exceptions

Exceptions are an important part of programming in SML, because they let us quickly and easily break out of exceptional conditions. We have seen them primarily in terms of the div function, which we know raises an exception on dividing by zero.

In this lecture, we discussed exceptions in more generality, by learning about the Match and Bind top-level exceptions, which are raised on entering a nonexhaustive match, and attempting a nonexhaustive binding, respectively.

We then talked about exception handling, which offers us a way to actually recover from a raised exception, rather than always crashing the program. We saw that exception handling took the form of pattern matching on the particular exception raised, which is just a value of the extensible type exn, called extensible because new variants can be added to it at any time.

We then explored exception-handling style, which is a less useful alternative to continuation-passing style, in which instead of dictating our control flow by invoking directly-passed continuations, we raise exceptions which are later handled to create branching behavior.