Programmers want to control everything within their programs. In Haskell, we can do that via type discipline and reasoning about function behavior. Unfortunately, communication with the outer world sets limits to that control as follows:
- When reading a file, we can never be sure in advance whether it exists.
- Even if it does exist, we have no guarantee its content matches our expectations.
- While we are fetching a web page, a network connection may break.
- Acquiring a resource (even memory allocation) may fail.
How should we react? When can we get information about the problems we have? When do we know how to address them? Are these at the same points in a program or later? Should we halt our application completely? Is it possible to figure out how to proceed instead? These questions have to be answered in every application.
In this chapter, we’ll discuss the idea of exceptions and exception-handling mechanisms for getting around these problems. We’ll also cover logging as an essential tool for recording information on what is going wrong at run time.