[Haskell] Better Exception Handling

Scott Turner p.turner at computer.org
Wed Nov 24 14:16:15 EST 2004


On 2004 November 24 Wednesday 13:12, Jules Bean wrote:
> On 24 Nov 2004, at 16:21, Scott Turner wrote:
> > On 2004 November 23 Tuesday 10:51, John Goerzen wrote:
> > The way to deal with those kinds of details is to use Either in a
> > monad. 

> Ok, I glanced through your code, and you seem to be reimplementing many
> of the ideas in the MonadError class, which also makes Either into a
> Monad.

Yes, it's a case of reinventing the wheel, the addition being as you point 
out, the support for a hierarchy of exceptions.  My code could be simplified 
by building on MonadError rather than doing the monad from scratch.

> Would you care to clarify the important differences? It looks like your
> support for hierarchies of exceptions is an enhancement? 

The hierarchy of types that can catch the exception is determined at the point 
where the exception is raised.  So the error value is represented in the 
monad using a list of Dynamic values, lazily.  In the past I have avoided 
'Dynamic', but it's needed here if a perfect match between the catch and the 
raising of the exception is to be detected based on type -- this is what 
programmers have come to expect.

Each error type is an instance of Hierarchical, so that its errors may be 
considered part of a larger category of errors.  In the instance definition, 
'parent' specifies how the error appears if it is caught by a handler 
expecting then next more general error type.


More information about the Haskell mailing list