There is flexible exception handling in Haskell?

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, 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? Thanks. --------------------------------------------- Este mensaje lo ha enviado un Alumno de la Universidad de Malaga. http://www.alumnos.uma.es/

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.

On Tue, Nov 18, 2003 at 12:40:45PM +1100, Bernard James POPE wrote:
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.
You could also check out System.IO.Error, which allows you to define (and of course, raise) IOErrors, which is probably closer to what you want (see mkIOError). If you use System.Exception to define errors of type Exception, then you'll need to use System.Exception.catch to catch them. If you define errors of type ioError, then the prelude catch will catch them, which is more likely what you want. -- David Roundy http://www.abridgegame.org

Am Dienstag, 18. November 2003, 02:40 schrieb Bernard James POPE:
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 ...
I think, it's better to combine all the different error cases like: data Result = Ok Int | Error error data Error = ThisError | ThatError String | SomeError Int ... It can get cumbersome if you always have to deal with errors explicitely in your code instead of just doing your calculations and letting the run-time system or whatever deal with the errors, so you might be tempted to use exceptions. But have a look at the module Control.Monad.Error. It provides you with support for dealing with exceptional situations without using the IO monad or special run-time system support.
[...]
Wolfgang
participants (4)
-
7253146@informatica.alumnos.uma.es
-
Bernard James POPE
-
David Roundy
-
Wolfgang Jeltsch