[Haskell-cafe] Exceptions

John Goerzen jgoerzen at complete.org
Fri Oct 1 11:43:58 EDT 2004

One of the most important features of a modern language is, to me,
exceptions.  I have found very little coverage of exceptions in Haskell,
what what coverage there was seemed to be centered on I/O.

One reason I'm asking is this: what to do when pattern matching is
incomplete due to an error.  For instance, this is a common pattern in
my OCaml code:

let queuedir_of_type t = match t with
  "foo" -> "/data/queue5"
| "bar" -> "/data/queue3"
| _ -> raise (Exception ("Invalid type " ^ t));;

This is often useful when "t" is something that was originally read from
a user.

Python has an expressive exception mechanism, where one can catch
various different exceptions from a single block of code, etc.

So, I have some questions about exceptions in Haskell:

1. Can exceptions be used in "pure" functions (outside of monads?)

2. How are these exceptions caught and handled aside from using bracket?

3. Can I define my own exception types?

4. Can I write code that can catch and handle multiple different
exception types from a single block of code?

5. Is there anything different about working with exceptions in monads?

6. Can I get a stack trace from ghc or hugs if an exception is never
caught and thus causes the program to terminate?


-- John

More information about the Haskell-Cafe mailing list