summaryrefslogtreecommitdiff
path: root/libraries/base/tests/exceptionsrun001.hs
blob: 46ab9fa6cc489399662fa44da3c3fdedf0fada8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module Main where

import Control.Exception
import System.IO.Error

main = do
  ioTest
  errorTest
  noMethodTest
  patMatchTest
  guardTest

ioTest :: IO ()
ioTest = catchJust (\e -> if isUserError e then Just () else Nothing)
                   (ioError (userError "wibble"))
                   (\() -> putStrLn "user exception caught")

errorTest :: IO ()
errorTest = do r <- try (evaluate (1 + error "call to 'error'"))
               case r of
                   Left (ErrorCall _) -> putStrLn "error call caught"
                   Right _            -> error "help!"

instance (Show a, Eq a) => Num (Maybe a) where {}

noMethodTest :: IO ()
noMethodTest = do r <- try (evaluate (Just () + Just ()))
                  case r of
                      Left (NoMethodError err) -> putStrLn "no method error"
                      Right _                  -> error "help!"

patMatchTest :: IO ()
patMatchTest = catch (case test1 [1..10] of () -> return ())
  (\ex -> case ex of
          PatternMatchFail err -> putStr err
          _                    -> error "help!")

test1 [] = ()

guardTest = catch (case test2 of () -> return ())
                  (\ex -> case ex of
                          PatternMatchFail err -> putStr err
                          _                    -> error "help!")

test2 | all (==0) [1] = ()