15-150: Principles of Functional Programming

Lecture 12: Exceptions

Continuing on from our discussion of continuation-passing style and its implications for control flow, we discussed exceptions in more depth. While we have seen exceptions before, we have not given them much consideration, and only used them for anomalous cases where we cannot continue.

We introduced the different kinds of built-in exceptions in SML, which include Bind, Match, and Div. We also saw the handle keyword, which allows us to recover from raised exceptions, giving us additional power when dealing with exceptions. We also saw that we could define our own custom exceptions which can be used for our own purposes, including exceptions which themselves carry data.

Seen in this way, exceptions become a way of enabling nonlocal control flow, where we can jump between different contexts of a program. This lets us establish sophisticated programs which are allowed to continue in exceptional contexts, without crashing outright.

We saw that, similarly to how we can write continuation-passing style code which use continuations to explicitly build up control flow, we are able to write exception-handling style code which uses exceptions to pass data around and direct the execution of the program.

Slides