# [Haskell-cafe] testing for exceptions

Darren Grant dedgrant at gmail.com
Sat Oct 31 14:39:26 UTC 2015

```Good clarification.

Cheers,
Darren
On Nov 1, 2015 01:09, "Erik Hesselink" <hesselink at gmail.com> wrote:

> I think you misinterpreted that answer. As far as I'm aware, `error`
> (except in its type) is no less reasonable than other exceptions, and
> you can catch it in IO. So as long as you run a test in IO, you can
> test for the call to `error` (as Joachim showed in another reply).
>
> Regards,
>
> Erik
>
> On 31 October 2015 at 11:40, Darren Grant <dedgrant at gmail.com> wrote:
> > Unfrotunately the answer to this is not simple:
> >
> >
> http://stackoverflow.com/questions/4243117/how-to-catch-and-ignore-a-call-to-the-error-function
> >
> > 'error' more or less terminates the program in an unreasonable way.
> >
> > It would be preferable for f2 to result in a type that can contain the
> error
> > result to be parsed.
> >
> > Cheers,
> > Darren
> >
> > On Oct 31, 2015 21:16, "Roelof Wobben" <r.wobben at home.nl> wrote:
> >>
> >> Hello,
> >>
> >> I have made this exercise which can be found at the Craft of Functional
> >> Programming  book.
> >>
> >> -- exercise 32
> >>
> >> -- Suppose we have to raise 2 to the power n. If n is even, 2*m say,
> then
> >> -- 2n = 22*m = (2m)2
> >> -- If n is odd, 2*m+l say, then
> >> -- 2n = 22*m+l = (2n)2*2
> >> -- Give a recursive function to compute 2n which uses these insights.
> >>
> >> f2 :: Integer -> Integer
> >> f2 n
> >>   | n < 0   = error "This will only run for positive numbers"
> >>   | n == 0  = 1
> >>   | even n = f2 ( n `div` 2) ^ 2
> >>   | odd n  = (f2 ( n `div` 2) ^ 2) * 2
> >>
> >>
> >> Now I have to make Hunit tests for it,
> >>
> >> But is there a way I can test if the error message is shown when a
> >> negative number is being used ?
> >>
> >> Roelof
> >>
> >> _______________________________________________
> >> Haskell-Cafe mailing list
> >
> >
> > _______________________________________________
> > Haskell-Cafe mailing list