How do I create an IOError exception?

Bayley, Alistair Alistair_Bayley@ldn.invesco.com
Thu, 5 Jun 2003 08:02:09 +0100


Ahh... I see now (thanks). This advice (in the documentation for
System.IO.Error.ioError) threw me:

"The ioError variant should be used in preference to throw to raise an
exception within the IO monad because it guarantees ordering with respect to
other IO operations, whereas throw does not."

After reading this I assumed that I should also use the exceptions in
System.IO.Error i.e. the IOError type. But of course this is not the case;
ioError has type Exception -> IO a.


-----Original Message-----
From: Hal Daume III [mailto:hdaume@ISI.EDU]
Sent: 04 June 2003 16:32
To: Bayley, Alistair
Cc: haskell-cafe@haskell.org
Subject: Re: How do I create an IOError exception?


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.
*****************************************************************