[Haskell-cafe] Idiomatic error handling in Haskell

Henning Thielemann lemming at henning-thielemann.de
Wed Mar 2 13:39:11 CET 2011


On Wed, 2 Mar 2011, Rouan van Dalen wrote:

> I would like to know what is the preferred Haskell mechanism for 
> handling exceptions in the IO monad?  I am not concerned with mechanisms 
> such as Maybe / Either, but would like to know about exception 
> mechanisms inside the IO monad.
>
> The 2 I know of are:
>  o) throwDyn
>  o) ioError and catch
>
> I do need the exceptions to be extendable.  So which is the preferred way
> to handle exceptions in Haskell for new libs?

For my taste there is nothing I can really recommend. I cannot recommend 
the exceptions that are implicit in IO, since if you have an IO action you 
cannot know on what exceptions you have to react. Can the action throw a 
FileDoesNotExist or AudioBufferCouldNotBeAllocated? You don't know, 
because the type does not tell you. I think the mechanisms like IO (Either 
ErrorMsg a) are the better solution, since they explicitly tell you, what 
exceptions you have to expect and when you forgot to handle a particular 
exception. I think the control-monad-exception package is the most 
advanced one, but it requires some type extensions. I have written 
explicit-exceptions which is Haskell 98, and you can use more advanced 
types of exceptions (such as those from control-monad-exception) on top of 
it.



More information about the Haskell-Cafe mailing list