File: exceptionsrun001.hs

package info (click to toggle)
ghc 9.0.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 177,780 kB
  • sloc: haskell: 494,441; ansic: 70,262; javascript: 9,423; sh: 8,537; python: 2,646; asm: 1,725; makefile: 1,333; xml: 196; cpp: 167; perl: 143; ruby: 84; lisp: 7
file content (46 lines) | stat: -rw-r--r-- 1,332 bytes parent folder | download | duplicates (10)
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] = ()