[Haskell-cafe] Use unsafePerformIO to catch Exception?

Xiao-Yong Jin xj2106 at columbia.edu
Mon Mar 23 08:11:47 EDT 2009


I just feel it is not comfortable to deal with exceptions
only within IO monad, so I defined

> tryArith :: a -> Either ArithException a
> tryArith = unsafePerformIO . try . evaluate

and it works quite good as

> map (tryArith . (div 5)) [2,1,0,5]

evaluates to

> [Right 2,Right 5,Left divide by zero,Right 1]

However, I guess unsafePerformIO definitely has a reason for
its name.  As I read through the document in
System.IO.Unsafe, I can't convince myself whether the use of
'tryArith' is indeed safe or unsafe.

I know there have been a lot of discussion around
unsafePerformIO, but I still can't figure it out by myself.
Can someone share some thoughts on this particular use of
unsafePerformIO?  Is it safe or not?  And why?

    c/*    __o/*
    <\     * (__
    */\      <

More information about the Haskell-Cafe mailing list