[Haskell-cafe] exceptions vs. Either
mailinglists at bjoernknafla.de
Tue Aug 3 07:05:13 EDT 2004
Hi - I am just learning Haskell and am far away from exception handling
intricacies. However I just recently read an article of Herb Sutter
about exception handling in C++ with some rules when to use exception
handling - and perhaps these rules might be applicable to Haskell too
(article: "When and How to Use Exceptions", Herb Sutter, C/C++ Users
Journal August 2004, pp. 47 -- 51)?
Herb Sutter gave these rules :
An error is any failure that prevents a function from succeeding. Three
main kind of errors:
- a condition that prevents the function from meeting a precondition of
another function that must be called (so to say: the caller has to
check for preconditions while the function called my use C/C++ asserts
to assert the correctness of its preconditions)
- a condition that prevents a function from establishing one of its own
postconditions (e.g. producing a (valid) return value)
- a condition that prevents the function from reestablishing an
invariant that it is responsible to maintain (special kind of
postcondition mainly found in object oriented code).
Any other condition is not an error and shouldn't be reported as one.
The code that could cause an error is responsible for detecting and
reporting the error otherwise this is a programming mistake.
I am not sure if these rules apply to real functional programming but
at least they seem to be useable. The point that the caller is
responsible for checking the preconditions of functions it is calling
is something I also found as a suggestion in using the object oriented
Cheers and best regards
More information about the Haskell-Cafe