[Haskell-cafe] Re: Exception handling in numeric computations

Xiao-Yong Jin xj2106 at columbia.edu
Thu Mar 26 11:47:14 EDT 2009

John Lato <jwlato at gmail.com> writes:

> Even in Haskell this separation isn't absolute.  Programmer errors,
> such as dividing by 0, can and do lead to exceptional conditions.  The
> proper way to handle dividing by 0 is to not do it in the first place,
> but if it happens because of a programming error, you've got an
> exception.  Unfortunately this encourages programmers to think that
> handling the exception is the proper way to deal with this condition,
> but it isn't.

So I have another question.  Is the following function safe
and legitimate?

> safeDiv :: (Exception e, Integral a) =>
>            a -> a -> Either e a
> safeDiv x y = unsafePerformIO . try . evaluate $ div x y

I believe it should be okay to use this 'safeDiv'.  What do
you think?
    c/*    __o/*
    <\     * (__
    */\      <

More information about the Haskell-Cafe mailing list