[Haskell] Better Exception Handling

John Goerzen jgoerzen at complete.org
Tue Nov 23 14:04:41 EST 2004

On Tue, Nov 23, 2004 at 05:20:19PM +0000, Ben Rudiak-Gould wrote:
> >So what am I missing here?
> myfunc might raise more than one exception. For example,
>    myfunc = error "x" + error "y"

Gotcha.  That's the piece I was missing!

[ snip ]

> those I catch. If each particular implementation were just consistent 
> about which exception it reports in these situations, I think the 
> exception-catching functions would be pure. In any case, mapException is 
> pure, and it's good enough for most of the cases where one might want to 
> catch exceptions outside the IO monad.

Well, I'm maving trouble wrapping my head around how I could use it in a
pure enviroment.  It's defined as:

mapException :: (Exception -> Exception) -> a -> a

I *think*, from reading the source, that it is returning the value 'a'
if there is no exception, or the mapped exception (or set of them?) if
there is.

That doesn't really help me, though, because I still have an exception
that I must catch in the IO monad.

> The second problem with exceptions in Haskell, which I think is rather 
> more serious, is that they can hide inside of data structures. This 

I've thought about some, and really, that doesn't bother me.  In
practice, if I never access the part of the structure with the
exception, I probably never care that the parsing failed.  In a
well-written imperative program, I would have never called the parser in
this situation.

> means that you can't, for example, wrap an exception handler around 
> (readConfigFile :: FilePath -> IO [ConfigOption]) and be sure that you 

Is someone else working on configuration file parsing, or are you just
remembering my earlier question? :-)

> will catch any exceptions that are thrown during the parsing of the 
> configuration file. Depending on how readConfigFile is written, those 
> exceptions may be hiding inside the returned list, where they won't be 
> noticed until some other part of the program inspects that part of the 
> list, at which point your exception handler is no longer in scope. The 

I lost you here.  Isn't the exception handler just another function?
That is, closures would handle this just like anything else?

> A question I don't know the answer to is whether (catchJust :: a -> 
> Maybe a), which lumps all exceptions into a Nothing return, is pure. It 
> seems like it would be; if so, why don't we have it? Or do we?

Are you proposing a hypothetical here?  (GHC's catchJust returns an
IO a).

-- John

More information about the Haskell mailing list