Haskell Exceptions
July 18, 2009 at 6:36 pm | In haskell | Leave a CommentTags: error handling, haskell
Haskell’s absolute worst feature, in my opinion, is its system of exceptions. I have yet to see a single use of exceptions that wouldn’t be better served by the use of a “Maybe” or an “Either” type. Luckily, in a small nod to sanity, Haskell provides ‘Control.Exception.Base.try’, which returns an Either type, with the exception left, and the value right.
Once you have that, it becomes easy to implement some sane functionality for exception handling, such as my current favorite functions, ‘defaultOnError’ and ‘errorToMaybe’:
defaultOnError :: a -> IO a -> IO a
defaultOnError d a = do tryValue <- try a
case tryValue of
Left _ -> return d
Right v -> return v
errorToMaybe :: IO a -> IO (Maybe a)
errorToMaybe a = do tryValue <- try a
case tryValue of
Left _ -> return $ Nothing
Right v -> return $ Just v
No Comments Yet »
RSS feed for comments on this post. TrackBack URI
Leave a comment
Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.