[Haskell] Better Exception Handling

Bayley, Alistair Alistair_Bayley at ldn.invesco.com
Tue Nov 23 10:43:09 EST 2004

> From: John Goerzen [mailto:jgoerzen at complete.org] 
> My other choice is to use Dynamic for my
> exceptions, but that makes it even more 
> difficult to catch and handle

Here's how I create "custom" exceptions; it doesn't seem onerous to me, but
then I have a high tolerance for some kinds of coding pain:

> data SqliteException = SqliteException Int String
>   deriving (Typeable)

> catchSqlite :: IO a -> (SqliteException -> IO a) -> IO a
> catchSqlite = catchDyn

> throwSqlite :: SqliteException -> a
> throwSqlite = throwDyn

> Python can work that way, but also adds another feature:
> try:
>     blah
>     moreblah
> finally:
>     foo

And in Haskell we have catch(Dyn), bracket, and finally. Are these not

> Haskell's exception catching doesn't really have less 
> functionality than OCaml (some might argue it has more),
> but it takes a lot more effort to compose, needing
> to provide a function that returns a function in many cases.

Does it? I'm not convinced... I think it's no more verbose than any other
exception-handling mechanism, but maybe there's some cognitive overhead in
translating an OO exception-handling idiom into Haskell. All I've ever used
is catch and bracket, and I find them fairly straightforward. Could you post
some code which you think would be clearer with an Ocaml or Python exception
handling style?

> The other annoying thing is forcing it to run in the IO monad. 

Note that you can throw exceptions from anywhere. They just have to be
*caught* in the IO monad.


Confidentiality Note: The information contained in this 
message, and any attachments, may contain confidential 
and/or privileged material. It is intended solely for the 
person(s) or entity to which it is addressed. Any review, 
retransmission, dissemination, or taking of any action in 
reliance upon this information by persons or entities other 
than the intended recipient(s) is prohibited. If you received
this in error, please contact the sender and delete the 
material from any computer.

More information about the Haskell mailing list