[Haskell] Better Exception Handling

Peter Strand peter at zarquon.se
Tue Nov 23 17:41:27 EST 2004


John Goerzen wrote:
>>>Python can work that way, but also adds another feature:
>>>
>>>try:
>>>    blah
>>>    moreblah
>>>finally:
>>>    foo
>>
>>And in Haskell we have catch(Dyn), bracket, and finally. Are these not
>>enough?
> 
> I hadn't been aware of finally.  That does seem to help.


One of the things I like about exceptions in haskell is that there's
relatively little magic, if a function like finally isn't there, you
can implement it yourself.

m `finally` f = do
     r <- m `catch` (\e -> f >> throw e)
     f
     return r


(I'm ignoring asynchronous exceptions for now, see the code for
  Control.Exception for details)


And I find bracket quite useful, it nicely captures a concept you have
to code "by hand" in many other languages. And bracket is also a
function you could have implemented yourself, if it hadn't been
available.



/Peter


More information about the Haskell mailing list