1 % -----------------------------------------------------------------------------
2 % $Id: PrelException.lhs,v 1.4 1999/01/14 18:12:57 sof Exp $
4 % (c) The GRAP/AQUA Project, Glasgow University, 1998
7 Exceptions and exception-handling functions.
10 {-# OPTIONS -fno-implicit-prelude #-}
13 module PrelException where
17 import PrelST ( STret(..) )
23 -----------------------------------------------------------------------------
24 Exception datatype and operations.
28 = IOException IOError -- IO exceptions (from 'ioError')
29 | ArithException ArithException -- Arithmetic exceptions
30 | ErrorCall String -- Calls to 'error'
31 | NoMethodError String -- A non-existent method was invoked
32 | PatternMatchFail String -- A pattern match failed
33 | NonExhaustiveGuards String -- A guard match failed
34 | RecSelError String -- Selecting a non-existent field
35 | RecConError String -- Field missing in record construction
36 | RecUpdError String -- Record doesn't contain updated field
37 | AssertionFailed String -- Assertions
38 | DynException Dynamic -- Dynamic exceptions
39 | AsyncException AsyncException -- Externally generated errors
55 instance Show ArithException where
56 showsPrec _ Overflow = showString "arithmetic overflow"
57 showsPrec _ Underflow = showString "arithmetic underflow"
58 showsPrec _ LossOfPrecision = showString "loss of precision"
59 showsPrec _ DivideByZero = showString "divide by zero"
60 showsPrec _ Denormal = showString "denormal"
62 instance Show AsyncException where
63 showsPrec _ StackOverflow = showString "stack overflow"
64 showsPrec _ HeapOverflow = showString "heap overflow"
65 showsPrec _ ThreadKilled = showString "thread killed"
67 instance Show Exception where
68 showsPrec _ (IOException err) = shows err
69 showsPrec _ (ArithException err) = shows err
70 showsPrec _ (ErrorCall err) = showString err
71 showsPrec _ (NoMethodError err) = showString err
72 showsPrec _ (PatternMatchFail err) = showString err
73 showsPrec _ (NonExhaustiveGuards err) = showString err
74 showsPrec _ (RecSelError err) = showString err
75 showsPrec _ (RecConError err) = showString err
76 showsPrec _ (RecUpdError err) = showString err
77 showsPrec _ (AssertionFailed err) = showString err
78 showsPrec _ (AsyncException e) = shows e
79 showsPrec _ (DynException _err) = showString "unknown exception"
83 throw :: Exception -> a
88 throw exception = raise# exception
92 catch handles the passing around of the state in the IO monad; if we
93 don't actually apply (and hence run) an IO computation, we don't get
94 any exceptions! Hence a large mantrap to watch out for is
96 catch# (m :: IO ()) (handler :: NDSet Exception -> IO ())
98 since the computation 'm' won't actually be performed in the context
99 of the 'catch#'. In fact, don't use catch# at all.
102 catchException :: IO a -> (Exception -> IO a) -> IO a
104 catchException m k = ST (\s -> unST m s `primCatch'` \ err -> unST (k err) s)
106 catchException m k = IO $ \s -> case catch# (liftIO m s) (\exs -> liftIO (k exs) s)
107 of STret s1 r -> (# s1, r #)
110 catch :: IO a -> (IOError -> IO a) -> IO a
111 catch m k = catchException m handler
112 where handler (IOException err) = k err
113 handler other = throw other
116 Why is this stuff here? To avoid recursive module dependencies of
120 ioError :: IOError -> IO a
121 ioError err = throw (IOException err)