How do I create an IOError exception?

Hal Daume III hdaume@ISI.EDU
Wed, 4 Jun 2003 08:31:31 -0700 (PDT)


In Control.Exception, there's:

> data Exception = ...
>                | DynException Dynamic
>                | ...

and

> throwIO :: Exception -> IO a

so, what you want is probably something like:

> if rc < 0
>   then throwIO (DynException (toDyn ("Err", rc)))
>   else ...

and then when you catch, use fromDyn(amic) to get the value out if you can
predict its type...

HTH.

 - Hal

--
 Hal Daume III                                   | hdaume@isi.edu
 "Arrest this man, he talks in maths."           | www.isi.edu/~hdaume

On Wed, 4 Jun 2003, Bayley, Alistair wrote:

> (I know I'm asking some noddy questions, but hey, that's what this list is
> for...)
> 
> How do I create IOError exceptions? System.IO.Error has two functions that
> create IOErrors: userError and mkIOError. However, both of them take a
> String (I assume containing a description of the problem), and mkIOError
> takes optional Handle and FilePath args.
> 
> http://www.haskell.org/ghc/docs/latest/html/base/System.IO.Error.html
> 
> What I'd like to do is stuff some more information into the exception (a bit
> like Java exceptions), along the lines of:
> 
> > mkIOError :: a -> IOError
> 
> where I could choose to use it like this (ociHandleAlloc is a foreign C
> function):
> 
> > handleAlloc2 handleType env ptr = do
> >	rc <- ociHandleAlloc env ptr handleType 0 0
> >	if rc < 0
> >		then ioError (mkIOError ("Couldn't allocate handle", rc))
> >		else peek ptr
> 
> 
> When I catch the exception (currently in main) I would like to interrogate
> it to get the values I stuffed in it (in this case a (String, Int) tuple),
> and then do some error reporting by calling another function which decodes
> the error number.
> 
> Is this the wrong way to go about error handling? Or is it relatively simple
> to create your own IOErrors? Have I missed something in the docs?
> 
> 
> *****************************************************************
> The information in this email and in any attachments is 
> confidential and intended solely for the attention and use 
> of the named addressee(s). This information may be 
> subject to legal professional or other privilege or may 
> otherwise be protected by work product immunity or other 
> legal rules.  It must not be disclosed to any person without 
> our authority.
> 
> If you are not the intended recipient, or a person 
> responsible for delivering it to the intended recipient, you 
> are not authorised to and must not disclose, copy, 
> distribute, or retain this message or any part of it.
> *****************************************************************
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>