[Haskell-cafe] testing for exceptions

joachifm at fastmail.fm joachifm at fastmail.fm
Sat Oct 31 10:37:18 UTC 2015


On Sat, Oct 31, 2015, at 11:16 AM, Roelof Wobben wrote:
> [...]
> But is there a way I can test if the error message is shown when a 
> negative number is being used ?

Something like (untested)

```
import qualified Control.Exception as E

isErrorCall :: String -> a -> IO Bool
isErrorCall s x = (E.evaluate x >> return False)
                  `E.catch` (\(E.ErrorCall e) -> return $ e == s)

myTest = isErrorCall "This will only run for positive numbers" . f2

{-
>> myTest (-1) = return True
>> myTest 100 = return False
-}
```

HTH,

Joachim


More information about the Haskell-Cafe mailing list