1 % -----------------------------------------------------------------------------
2 % $Id: PrelException.lhs,v 1.12 2000/03/13 10:54:49 simonmar 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
58 stackOverflow, heapOverflow :: Exception -- for the RTS
59 stackOverflow = AsyncException StackOverflow
60 heapOverflow = AsyncException HeapOverflow
62 instance Show ArithException where
63 showsPrec _ Overflow = showString "arithmetic overflow"
64 showsPrec _ Underflow = showString "arithmetic underflow"
65 showsPrec _ LossOfPrecision = showString "loss of precision"
66 showsPrec _ DivideByZero = showString "divide by zero"
67 showsPrec _ Denormal = showString "denormal"
69 instance Show AsyncException where
70 showsPrec _ StackOverflow = showString "stack overflow"
71 showsPrec _ HeapOverflow = showString "heap overflow"
72 showsPrec _ ThreadKilled = showString "thread killed"
74 instance Show Exception where
75 showsPrec _ (IOException err) = shows err
76 showsPrec _ (ArithException err) = shows err
77 showsPrec _ (ErrorCall err) = showString err
78 showsPrec _ (NoMethodError err) = showString err
79 showsPrec _ (PatternMatchFail err) = showString err
80 showsPrec _ (NonExhaustiveGuards err) = showString err
81 showsPrec _ (RecSelError err) = showString err
82 showsPrec _ (RecConError err) = showString err
83 showsPrec _ (RecUpdError err) = showString err
84 showsPrec _ (AssertionFailed err) = showString err
85 showsPrec _ (AsyncException e) = shows e
86 showsPrec _ (DynException _err) = showString "unknown exception"
87 showsPrec _ (PutFullMVar) = showString "putMVar: full MVar"
88 showsPrec _ (NonTermination) = showString "<<loop>>"
92 throw :: Exception -> a
97 throw exception = raise# exception
101 catchException used to handle the passing around of the state to the
102 action and the handler. This turned out to be a bad idea - it meant
103 that we had to wrap both arguments in thunks so they could be entered
104 as normal (remember IO returns an unboxed pair...).
108 catch# :: IO a -> (b -> IO a) -> IO a
110 (well almost; the compiler doesn't know about the IO newtype so we
111 have to work around that in the definition of catchException below).
114 catchException :: IO a -> (Exception -> IO a) -> IO a
116 catchException m k = ST (\s -> unST m s `primCatch'` \ err -> unST (k err) s)
118 catchException (IO m) k = IO $ \s -> catch# m (\ex -> unIO (k ex)) s
121 catch :: IO a -> (IOError -> IO a) -> IO a
122 catch m k = catchException m handler
123 where handler (IOException err) = k err
124 handler other = throw other
126 catchNonIO :: IO a -> (Exception -> IO a) -> IO a
127 catchNonIO m k = catchException m handler
128 where handler (IOException err) = ioError err
129 handler other = k other
133 Why is this stuff here? To avoid recursive module dependencies of
137 ioError :: IOError -> IO a
138 ioError err = throw (IOException err)