description: FP Complete's recommended practices for dealing with exceptions
first-written: 2015-02-24
last-updated: 2015-02-24
last-reviewed: 2015-02-24
---
This is an FP Complete coding standards document written by Michael Snoyman.
I'm exposing it to the outside world, but some of the prose definitely
maintains the coding standard approach. This piece is highly opinionated, and
I'm sure some people will have different thoughts on how to do this.
* * *
There is much debate in the Haskell community around exception handling. One
commonly stated position goes something like "all exceptions should be explicit
at the type level, and async exceptions are terrible." We can argue as much as
we want about this point in a theoretical sense. However, practically, it is
irrelevant, because GHC has already chosen a stance on this: it supports async
exceptions, and all code that runs in `IO` can have exceptions of *any* type
which is an instance of `Exception`.
As far as our coding standards go, we need to accept the world as it is, and
realize that any `IO` code can throw any exception. (We can also discuss the
theoretical benefits of the chosen setup, versus the terrible situation of
checked exceptions in Java, but that's really a separate matter.) Additionally,
all code must be written to be async-exception safe. How this is done is *not*
covered in this document.
Let's identify a few anti-patterns in Haskell exception handling, and then move
on to recommended practices.
## The bad
### ExceptT IO anti-pattern
A common (bad) design pattern I see is something like the following:
```haskell
myFunction :: String -> ExceptT MyException IO Int
```
There are (at least) three problems with this:
1. It's *non-composable*. If someone else has a separate exception type `HisException`, these two functions do not easily compose.
2. It gives an implication which is almost certainly false, namely: the only exception that can be thrown from this function is `MyException`. Almost any `IO` code in there will have the ability to throw some other type of exception, and additionally, almost any async exception can be thrown even if no synchronous exception is possible.
3. You haven't limited the possibility of exceptions, you've only added one extra avenue by which an exception can be thrown. `myFunction` can now either `throwE` or `liftIO . throwIO`.
It is almost always wrong to wrap an `ExceptT`, `EitherT`, or `ErrorT` around an `IO`-based transformer stack.
Separate issue: it's also almost always a bad idea to have such a concrete
transformer stack used in a public-facing API. It's usually better to express a
function in terms of typeclass requirements, using mtl typeclasses as
necessary.
### Mask-them-all anti-pattern
This anti-pattern goes like this: remembering to deal with async exceptions everywhere is hard, so I'll just mask them all.
Every time you do this, 17 kittens are mauled to death by the loch ness monster.
Async exceptions may be annoying, but they are vital to keeping a system
functioning correctly. The `timeout` function uses them to great benefit. The
Warp webserver bases all of its slowloris protection on async exceptions. The
cancel function from the async package will hang indefinitely if async
exceptions are masked. Et cetera et cetera.
Are async exceptions difficult to work with? Sometimes, yes. Deal with it anyway. Best practices include:
* Use the bracket pattern wherever possible.
* If you have truly complex flow of control and non-linear scoping of resources, use the resourcet package.
## The good
### MonadThrow
Consider the following function:
```haskell
foo <-lookup"foo"m
bar <-lookup"bar"m
baz <-lookup"baz"m
f foo bar baz
```
If this function returns `Nothing`, we have no idea why. It could be because:
1. "foo" wasn't in the map.
2. "bar" wasn't in the map.
3. "baz" wasn't in the map.
4.`f` returned `Nothing`.
The problem is that we've thrown away a lot of information by having our functions return `Maybe`. Instead, wouldn't it be nice if the types of our functions were:
```haskell
lookup :: Eq k => k -> [(k, v)] -> Either (KeyNotFound k) v
f :: SomeVal -> SomeVal -> SomeVal -> Either F'sExceptionType F'sResult
```
The problem is that these types don't unify. Also, it's commonly the case that