1 % -----------------------------------------------------------------------------
2 % $Id: PrelException.lhs,v 1.14 2000/03/23 17:45:31 simonpj 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
18 import PrelST ( STret(..) )
24 -----------------------------------------------------------------------------
25 Exception datatype and operations.
29 = IOException IOError -- IO exceptions (from 'ioError')
30 | ArithException ArithException -- Arithmetic exceptions
31 | ErrorCall String -- Calls to 'error'
32 | NoMethodError String -- A non-existent method was invoked
33 | PatternMatchFail String -- A pattern match failed
34 | NonExhaustiveGuards String -- A guard match failed
35 | RecSelError String -- Selecting a non-existent field
36 | RecConError String -- Field missing in record construction
37 | RecUpdError String -- Record doesn't contain updated field
38 | AssertionFailed String -- Assertions
39 | DynException Dynamic -- Dynamic exceptions
40 | AsyncException AsyncException -- Externally generated errors
41 | PutFullMVar -- Put on a full MVar
42 | BlockedOnDeadMVar -- Blocking on a dead MVar
59 stackOverflow, heapOverflow :: Exception -- for the RTS
60 stackOverflow = AsyncException StackOverflow
61 heapOverflow = AsyncException HeapOverflow
63 instance Show ArithException where
64 showsPrec _ Overflow = showString "arithmetic overflow"
65 showsPrec _ Underflow = showString "arithmetic underflow"
66 showsPrec _ LossOfPrecision = showString "loss of precision"
67 showsPrec _ DivideByZero = showString "divide by zero"
68 showsPrec _ Denormal = showString "denormal"
70 instance Show AsyncException where
71 showsPrec _ StackOverflow = showString "stack overflow"
72 showsPrec _ HeapOverflow = showString "heap overflow"
73 showsPrec _ ThreadKilled = showString "thread killed"
75 instance Show Exception where
76 showsPrec _ (IOException err) = shows err
77 showsPrec _ (ArithException err) = shows err
78 showsPrec _ (ErrorCall err) = showString err
79 showsPrec _ (NoMethodError err) = showString err
80 showsPrec _ (PatternMatchFail err) = showString err
81 showsPrec _ (NonExhaustiveGuards err) = showString err
82 showsPrec _ (RecSelError err) = showString err
83 showsPrec _ (RecConError err) = showString err
84 showsPrec _ (RecUpdError err) = showString err
85 showsPrec _ (AssertionFailed err) = showString err
86 showsPrec _ (AsyncException e) = shows e
87 showsPrec _ (DynException _err) = showString "unknown exception"
88 showsPrec _ (PutFullMVar) = showString "putMVar: full MVar"
89 showsPrec _ (BlockedOnDeadMVar) = showString "thread blocked indefinitely"
90 showsPrec _ (NonTermination) = showString "<<loop>>"
94 throw :: Exception -> a
99 throw exception = raise# exception
103 catchException used to handle the passing around of the state to the
104 action and the handler. This turned out to be a bad idea - it meant
105 that we had to wrap both arguments in thunks so they could be entered
106 as normal (remember IO returns an unboxed pair...).
110 catch# :: IO a -> (b -> IO a) -> IO a
112 (well almost; the compiler doesn't know about the IO newtype so we
113 have to work around that in the definition of catchException below).
116 catchException :: IO a -> (Exception -> IO a) -> IO a
118 catchException m k = ST (\s -> unST m s `primCatch'` \ err -> unST (k err) s)
120 catchException (IO m) k = IO $ \s -> catch# m (\ex -> unIO (k ex)) s
123 catch :: IO a -> (IOError -> IO a) -> IO a
124 catch m k = catchException m handler
125 where handler (IOException err) = k err
126 handler other = throw other
128 catchNonIO :: IO a -> (Exception -> IO a) -> IO a
129 catchNonIO m k = catchException m handler
130 where handler (IOException err) = ioError err
131 handler other = k other
135 Why is this stuff here? To avoid recursive module dependencies of
139 ioError :: IOError -> IO a
140 ioError err = IO $ \s -> throw (IOException err) s
141 -- (ioError e) isn't an exception; we only throw
142 -- the exception when applied to a world