[Haskell-cafe] Exception handling when using STUArray

Xiao-Yong Jin xj2106 at columbia.edu
Sat Mar 8 09:54:44 EST 2008

"Denis Bueno" <dbueno at gmail.com> writes:

> On Sat, Mar 8, 2008 at 12:29 AM, Xiao-Yong Jin <xj2106 at columbia.edu> wrote:
>>  I'm using STUArray in some of my time critical number
>>  crunching code.  I would like to know some way to catch the
>>  exceptions raised in the ST monad, ie. ArrayException.
> I am also using STUArray from some time-critical code; however, I
> don't deal with ArrayException, or any exceptions for that matter.
> What besides an out-of-bounds read or write might throw an
> ArrayException?  If it is out-of-bounds reading or writing, surely
> that indicates a bug in your program that you'd rather fix than catch
> the exception, no?

In my case, because I choose a index of the array according
to certain value in the array, if there is NaN or Infinity
in the array, the code breaks.  So I guess, to fix the code,
I would probably use isNaN/isInfinite and throw an exception
when that happens.

> Also, if you're using GHC, note that the ArrayException documentation says:
>     "(NOTE: GHC currently does not throw ArrayExceptions)."
>>  Looking through the Control.Exception module, I understand
>>  that those functions can only be used within IO monad.  So
> This isn't quite true, if I understand what you mean.  The function
> throw can be used from any code
> (http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Exception.html).
>  But you must catch exceptions in the IO monad.

Thanks for the clarification.  I didn't read the
documentation carefully.  I'll experiment on `throw', right

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

More information about the Haskell-Cafe mailing list