7 Error 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 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.

7.1 Overview of error-handling mechanisms in Haskell

Exceptions in Haskell are traditionally complicated. At least two reasons for that follow:

7.1.1 The idea of exceptions

 
 

7.1.2 To use or not to use?

 
 
 
 

7.1.3 Programmable exceptions vs. GHC runtime exceptions

 

7.2 Programmable exceptions in monad stacks

 
 

7.2.1 The ExceptT monad transformer

 
 
 
 

7.2.2 Example: Evaluating RPN expressions

 
 
 

7.3 GHC runtime exceptions

 
 

7.3.1 An idea of extensible exceptions

 
 

7.3.2 Throwing exceptions

 
 
 

7.3.3 Catching exceptions

 
 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage