[Haskell-beginners] Re: Catching Exceptions in Haskell

Peter Verswyvelen bugfact at gmail.com
Sat Feb 28 21:16:13 EST 2009


The 3rd exception is raised when (show m) is computed, since no
exception occurred when evaluating (testGet L.empty) itself.

When you call "evaluate a", it only forces "one level" of evaluation of a.


E.g. if you would run

> evaluate (1:error "2":[]) >>= print . head

this would print 1, and not give an error.


You can maybe understand this better when you change

> (res :: Either SomeException String) <- try $ evaluate (f x)

into

> (res :: Either SomeException String) <- try $ return (f x)

Now the 2nd exception won't be raised either: evaluate digged one
level into assert, and that causes an exception, but return will not
do that, it will just return the unevaluated thunk that wraps the
assert computation, and this thunk will get evaluated when you print
it

You can get the behavior what you expect by using some functions from
Control.Parallel.Strategies.

Change

(res :: Either SomeException String) <- try $ evaluate (f x)

into

(res :: Either SomeException String) <- try $ evaluate (f x `using` rnf)


Now all 3 will throw an exception, as you expected. Why? (a `using`
rnf) reduces its argument (a) to "head normal form", which basically
means it evaluating all sub-expressions at all levels.

So

> evaluate (1:error "2":[] `using` rnf) >>= print . head

will now give an error.

Cheers,
Peter Verswyvelen






On Sun, Mar 1, 2009 at 1:48 AM, Bjoern Brandenburg <bbb.lst at gmail.com> wrote:
>
> Ok, so I was able to extract a simpler program with the same symptoms.
>
> http://hpaste.org/fastcgi/hpaste.fcgi/view?id=1900#a1900
>
> Apparently the exception is only triggered when m is used in line 31.
> So I guess that evaluate in line 24 is not causing strict evaluation
> of the Data.Binary.Get monad, even though it is working for the error
> and assert tests.
>
> Why is that? Is that expected? If so, what am I doing wrong?
>
> Thanks,
> Bjoern
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners


More information about the Beginners mailing list