There is flexible exception handling in Haskell?

Bernard James POPE bjpop at cs.mu.OZ.AU
Tue Nov 18 12:40:45 EST 2003


> Hi all. I am porting to Haskell a small zlib-based library for .zip files (I 
> have not seen any released package for it, although it should very useful). The 
> matters come when I try to address exceptional conditions: all the library 
> functions return a integer code with OK/SOMEERROR meaning. The most natural way 
> to carry the exceptional situations should be raise IO exceptions, 

Sometimes you can just encode your exceptional values in some type. It tends to
be more declarative than throwing/catching exceptions:

   data Result = Ok Int | ThisError | ThatError String | SomeError Int ...

And you library functions can be:

   fun :: Foo -> Bar -> IO Result

drop the IO type if you don't need it.
  
> but here 
> comes the problem: how can I define new Exception codes, instead of raising 
> userError all the time? I think it makes sense for a library to raise 
> specialized exceptions, instead of userErrors.
> There is such a mechanism? Can someone help?

If the encoding doesn't somehow suit your needs then you could try GHC's 
exception extensions, which provide a much richer exception facility than
plain Haskell 98

Have a look at the module Control.Exception in the user docs. 

I think that very modern versions of Hugs support some of this extension too.

Cheers,
Bernie.


More information about the Haskell-Cafe mailing list