How do I create an IOError exception?

Bayley, Alistair Alistair_Bayley@ldn.invesco.com
Wed, 4 Jun 2003 16:16:40 +0100


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