7 Exceptions handling and logging
This chapter covers:
- An idea of exceptions in Haskell and when to use them
- Several exception handling mechanisms
- Designing exception handling in pure and impure code
Programmers want to control everything within their programs. In Haskell, we can do that via type discipline and reasoning about functions behavior. Unfortunately, communication with the outer world sets limits to that control:
- Whenever we read a file, we can’t be sure in advance whether it exists or not;
- Even if it does exist, we have no guarantee its content matches our expectations;
- While we are fetching a web page, our program may be a subject to a broken network connection;
- Acquiring a scarce resource can fail due to a shortage.
How should we react? When can we get information about the problems we’ve got? When do we know how to address them? Is it the same time or not? Should we halt our application completely? Is it possible to figure out how to proceed instead? These questions will need to be answered in every application.
In this chapter, we’ll discuss the concept of exception and exception handling mechanisms for getting around the problems mentioned above. We’ll also cover logging as a useful tool for recording information on what is going wrong at runtime.
Exceptions in Haskell is a traditionally complicated topic. There are several reasons for that: