[project @ 2000-03-13 10:54:49 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / PrelException.lhs
1 % -----------------------------------------------------------------------------
2 % $Id: PrelException.lhs,v 1.12 2000/03/13 10:54:49 simonmar Exp $
3 %
4 % (c) The GRAP/AQUA Project, Glasgow University, 1998
5 %
6
7 Exceptions and exception-handling functions.
8
9 \begin{code}
10 {-# OPTIONS -fno-implicit-prelude #-}
11
12 #ifndef __HUGS__
13 module PrelException where
14
15 import PrelBase
16 import PrelShow
17 import PrelIOBase
18 import PrelST           ( STret(..) )
19 import PrelDynamic
20 import PrelGHC
21 #endif
22 \end{code}
23
24 -----------------------------------------------------------------------------
25 Exception datatype and operations.
26
27 \begin{code}
28 data Exception
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   | NonTermination
43
44 data ArithException
45   = Overflow
46   | Underflow
47   | LossOfPrecision
48   | DivideByZero
49   | Denormal
50   deriving (Eq, Ord)
51
52 data AsyncException
53   = StackOverflow
54   | HeapOverflow
55   | ThreadKilled
56   deriving (Eq, Ord)
57
58 stackOverflow, heapOverflow :: Exception -- for the RTS
59 stackOverflow = AsyncException StackOverflow
60 heapOverflow  = AsyncException HeapOverflow
61
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"
68
69 instance Show AsyncException where
70   showsPrec _ StackOverflow   = showString "stack overflow"
71   showsPrec _ HeapOverflow    = showString "heap overflow"
72   showsPrec _ ThreadKilled    = showString "thread killed"
73
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>>"
89
90 -- Primitives:
91
92 throw :: Exception -> a
93
94 #ifdef __HUGS__
95 throw = primRaise
96 #else
97 throw exception = raise# exception
98 #endif
99 \end{code}
100
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...).
105
106 Now catch# has type
107
108     catch# :: IO a -> (b -> IO a) -> IO a
109
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).
112
113 \begin{code}
114 catchException :: IO a -> (Exception -> IO a) -> IO a
115 #ifdef __HUGS__
116 catchException m k =  ST (\s -> unST m s `primCatch'` \ err -> unST (k err) s)
117 #else
118 catchException (IO m) k =  IO $ \s -> catch# m (\ex -> unIO (k ex)) s
119 #endif
120
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
125
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
130 \end{code}
131
132
133 Why is this stuff here?  To avoid recursive module dependencies of
134 course.
135
136 \begin{code}
137 ioError         :: IOError -> IO a 
138 ioError err     =  throw (IOException err)
139 \end{code}
140