[mercury-users] exceptions considered bloody annoying

Ralph Becket rafe at cs.mu.OZ.AU
Tue Apr 16 12:22:14 AEST 2002


Michael Day, Monday, 15 April 2002:
> 
> It just feels like C++, where exceptions are easy to throw, easy to catch,
> but screw up interfaces in ways you would never expect.

Well, that's exceptions for you.

The trouble with exceptions is that except for very rare circumstances*,
when one is caught you generally don't know enough about the state of the
various structures you're interested to be able to guarantee their
integrity (or even their existence!)

Database people insist upon transactional integrity and have the
appropriate machinery (logging, optimistic concurrency etc.) to support
that goal.

I've argued before that this is the right sort of approach to take.  It
is more heavyweight than one might *think* necessary, but I'm inclined
to the opinion that that is mainly because people typically
underestimate how hard it is to deal with arbitrary exceptions and keep
a program running.

An alternative might be to take a monadic approach, whereby the monadic
bind operator makes sure all the state you're interested in is preserved
in some sane state when your program throws the monadic equivalent of an
exception (think of it as being similar to returning and handling error
codes rather than throwing exceptions.)

- Ralph

* For example, in Okasaki's "Purely Functional Data Structures", he
  suggests using using an exception to signal when an already-present
  member of a set data structure is being inserted.  If the exception is
  caught, the insert operation reduces to the identity function and no
  extra memory is allocated.  Otherwise the insert operation proceeds as
  per usual.

> Quick quiz: does anyone regularly catch exceptions other than at the very
> top level of their program? How often do people find themselves using
> "throw" compared to just "error"?

The classic example is web-server plug-ins.  You don't want your server
to fall over just because some third-party plug-in throws an exception.
You have to catch it, return the world to a sane state, and carry on.

- Ralph
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list