525dc6af9d1b93d4bb8fb4b8b51d5fd897eda3ba
[ghc-base.git] / Control / Exception / Base.hs
1 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
2
3 #include "Typeable.h"
4
5 -----------------------------------------------------------------------------
6 -- |
7 -- Module      :  Control.Exception.Base
8 -- Copyright   :  (c) The University of Glasgow 2001
9 -- License     :  BSD-style (see the file libraries/base/LICENSE)
10 -- 
11 -- Maintainer  :  libraries@haskell.org
12 -- Stability   :  experimental
13 -- Portability :  non-portable (extended exceptions)
14 --
15 -- Extensible exceptions, except for multiple handlers.
16 --
17 -----------------------------------------------------------------------------
18
19 module Control.Exception.Base (
20
21         -- * The Exception type
22 #ifdef __HUGS__
23         SomeException,
24 #else
25         SomeException(..),
26 #endif
27         Exception(..),
28         IOException,
29         ArithException(..),
30         ArrayException(..),
31         AssertionFailed(..),
32         AsyncException(..),
33
34 #if __GLASGOW_HASKELL__ || __HUGS__
35         NonTermination(..),
36         NestedAtomically(..),
37 #endif
38
39         BlockedIndefinitelyOnMVar(..),
40         BlockedIndefinitelyOnSTM(..),
41         Deadlock(..),
42         NoMethodError(..),
43         PatternMatchFail(..),
44         RecConError(..),
45         RecSelError(..),
46         RecUpdError(..),
47         ErrorCall(..),
48
49         -- * Throwing exceptions
50         throwIO,
51         throw,
52         ioError,
53 #ifdef __GLASGOW_HASKELL__
54         throwTo,
55 #endif
56
57         -- * Catching Exceptions
58
59         -- ** The @catch@ functions
60         catch,
61         catchJust,
62
63         -- ** The @handle@ functions
64         handle,
65         handleJust,
66
67         -- ** The @try@ functions
68         try,
69         tryJust,
70         onException,
71
72         -- ** The @evaluate@ function
73         evaluate,
74
75         -- ** The @mapException@ function
76         mapException,
77
78         -- * Asynchronous Exceptions
79
80         -- ** Asynchronous exception control
81         mask,
82 #ifndef __NHC__
83         mask_,
84         uninterruptibleMask,
85         uninterruptibleMask_,
86         MaskingState(..),
87         getMaskingState,
88 #endif
89
90         -- ** (deprecated) Asynchronous exception control
91
92         block,
93         unblock,
94         blocked,
95
96         -- * Assertions
97
98         assert,
99
100         -- * Utilities
101
102         bracket,
103         bracket_,
104         bracketOnError,
105
106         finally,
107
108 #ifdef __GLASGOW_HASKELL__
109         -- * Calls for GHC runtime
110         recSelError, recConError, irrefutPatError, runtimeError,
111         nonExhaustiveGuardsError, patError, noMethodBindingError,
112         nonTermination, nestedAtomically,
113 #endif
114   ) where
115
116 #ifdef __GLASGOW_HASKELL__
117 import GHC.Base
118 import GHC.IO hiding (finally,onException)
119 import GHC.IO.Exception
120 import GHC.Exception
121 import GHC.Show
122 -- import GHC.Exception hiding ( Exception )
123 import GHC.Conc
124 #endif
125
126 #ifdef __HUGS__
127 import Prelude hiding (catch)
128 import Hugs.Prelude (ExitCode(..))
129 import Hugs.IOExts (unsafePerformIO)
130 import Hugs.Exception (SomeException(DynamicException, IOException,
131                                      ArithException, ArrayException, ExitException),
132                        evaluate, IOException, ArithException, ArrayException)
133 import qualified Hugs.Exception
134 #endif
135
136 import Data.Dynamic
137 import Data.Either
138 import Data.Maybe
139
140 #ifdef __NHC__
141 import qualified IO as H'98 (catch)
142 import IO              (bracket,ioError)
143 import DIOError         -- defn of IOError type
144 import System          (ExitCode())
145 import System.IO.Unsafe (unsafePerformIO)
146 import Unsafe.Coerce    (unsafeCoerce)
147
148 -- minimum needed for nhc98 to pretend it has Exceptions
149
150 {-
151 data Exception   = IOException    IOException
152                  | ArithException ArithException
153                  | ArrayException ArrayException
154                  | AsyncException AsyncException
155                  | ExitException  ExitCode
156                  deriving Show
157 -}
158 class ({-Typeable e,-} Show e) => Exception e where
159     toException   :: e -> SomeException
160     fromException :: SomeException -> Maybe e
161
162 data SomeException = forall e . Exception e => SomeException e
163
164 INSTANCE_TYPEABLE0(SomeException,someExceptionTc,"SomeException")
165
166 instance Show SomeException where
167     showsPrec p (SomeException e) = showsPrec p e
168 instance Exception SomeException where
169     toException se = se
170     fromException = Just
171
172 type IOException = IOError
173 instance Exception IOError where
174     toException                     = SomeException
175     fromException (SomeException e) = Just (unsafeCoerce e)
176
177 instance Exception ExitCode where
178     toException                     = SomeException
179     fromException (SomeException e) = Just (unsafeCoerce e)
180
181 data ArithException
182 data ArrayException
183 data AsyncException
184 data AssertionFailed
185 data PatternMatchFail
186 data NoMethodError
187 data Deadlock
188 data BlockedIndefinitelyOnMVar
189 data BlockedIndefinitelyOnSTM
190 data ErrorCall
191 data RecConError
192 data RecSelError
193 data RecUpdError
194 instance Show ArithException
195 instance Show ArrayException
196 instance Show AsyncException
197 instance Show AssertionFailed
198 instance Show PatternMatchFail
199 instance Show NoMethodError
200 instance Show Deadlock
201 instance Show BlockedIndefinitelyOnMVar
202 instance Show BlockedIndefinitelyOnSTM
203 instance Show ErrorCall
204 instance Show RecConError
205 instance Show RecSelError
206 instance Show RecUpdError
207
208 catch   :: Exception e
209         => IO a         -- ^ The computation to run
210         -> (e -> IO a)  -- ^ Handler to invoke if an exception is raised
211         -> IO a
212 catch io h = H'98.catch  io  (h . fromJust . fromException . toException)
213
214 throwIO  :: Exception e => e -> IO a
215 throwIO   = ioError . fromJust . fromException . toException
216
217 throw    :: Exception e => e -> a
218 throw     = unsafePerformIO . throwIO
219
220 evaluate :: a -> IO a
221 evaluate x = x `seq` return x
222
223 assert :: Bool -> a -> a
224 assert True  x = x
225 assert False _ = throw (toException (UserError "" "Assertion failed"))
226
227 mask   :: ((IO a-> IO a) -> IO a) -> IO a
228 mask action = action restore
229     where restore act = act
230
231 #endif
232
233 #ifdef __HUGS__
234 class (Typeable e, Show e) => Exception e where
235     toException   :: e -> SomeException
236     fromException :: SomeException -> Maybe e
237
238     toException e = DynamicException (toDyn e) (flip showsPrec e)
239     fromException (DynamicException dyn _) = fromDynamic dyn
240     fromException _ = Nothing
241
242 INSTANCE_TYPEABLE0(SomeException,someExceptionTc,"SomeException")
243 INSTANCE_TYPEABLE0(IOException,iOExceptionTc,"IOException")
244 INSTANCE_TYPEABLE0(ArithException,arithExceptionTc,"ArithException")
245 INSTANCE_TYPEABLE0(ArrayException,arrayExceptionTc,"ArrayException")
246 INSTANCE_TYPEABLE0(ExitCode,exitCodeTc,"ExitCode")
247 INSTANCE_TYPEABLE0(ErrorCall,errorCallTc,"ErrorCall")
248 INSTANCE_TYPEABLE0(AssertionFailed,assertionFailedTc,"AssertionFailed")
249 INSTANCE_TYPEABLE0(AsyncException,asyncExceptionTc,"AsyncException")
250 INSTANCE_TYPEABLE0(BlockedIndefinitelyOnMVar,blockedIndefinitelyOnMVarTc,"BlockedIndefinitelyOnMVar")
251 INSTANCE_TYPEABLE0(BlockedIndefinitelyOnSTM,blockedIndefinitelyOnSTM,"BlockedIndefinitelyOnSTM")
252 INSTANCE_TYPEABLE0(Deadlock,deadlockTc,"Deadlock")
253
254 instance Exception SomeException where
255     toException se = se
256     fromException = Just
257
258 instance Exception IOException where
259     toException = IOException
260     fromException (IOException e) = Just e
261     fromException _ = Nothing
262
263 instance Exception ArrayException where
264     toException = ArrayException
265     fromException (ArrayException e) = Just e
266     fromException _ = Nothing
267
268 instance Exception ArithException where
269     toException = ArithException
270     fromException (ArithException e) = Just e
271     fromException _ = Nothing
272
273 instance Exception ExitCode where
274     toException = ExitException
275     fromException (ExitException e) = Just e
276     fromException _ = Nothing
277
278 data ErrorCall = ErrorCall String
279
280 instance Show ErrorCall where
281     showsPrec _ (ErrorCall err) = showString err
282
283 instance Exception ErrorCall where
284     toException (ErrorCall s) = Hugs.Exception.ErrorCall s
285     fromException (Hugs.Exception.ErrorCall s) = Just (ErrorCall s)
286     fromException _ = Nothing
287
288 data BlockedIndefinitelyOnMVar = BlockedIndefinitelyOnMVar
289 data BlockedIndefinitelyOnSTM = BlockedIndefinitelyOnSTM
290 data Deadlock = Deadlock
291 data AssertionFailed = AssertionFailed String
292 data AsyncException
293   = StackOverflow
294   | HeapOverflow
295   | ThreadKilled
296   | UserInterrupt
297   deriving (Eq, Ord)
298
299 instance Show BlockedIndefinitelyOnMVar where
300     showsPrec _ BlockedIndefinitelyOnMVar = showString "thread blocked indefinitely"
301
302 instance Show BlockedIndefinitely where
303     showsPrec _ BlockedIndefinitely = showString "thread blocked indefinitely"
304
305 instance Show Deadlock where
306     showsPrec _ Deadlock = showString "<<deadlock>>"
307
308 instance Show AssertionFailed where
309     showsPrec _ (AssertionFailed err) = showString err
310
311 instance Show AsyncException where
312     showsPrec _ StackOverflow   = showString "stack overflow"
313     showsPrec _ HeapOverflow    = showString "heap overflow"
314     showsPrec _ ThreadKilled    = showString "thread killed"
315     showsPrec _ UserInterrupt   = showString "user interrupt"
316
317 instance Exception BlockedOnDeadMVar
318 instance Exception BlockedIndefinitely
319 instance Exception Deadlock
320 instance Exception AssertionFailed
321 instance Exception AsyncException
322
323 throw :: Exception e => e -> a
324 throw e = Hugs.Exception.throw (toException e)
325
326 throwIO :: Exception e => e -> IO a
327 throwIO e = Hugs.Exception.throwIO (toException e)
328 #endif
329
330 #ifndef __GLASGOW_HASKELL__
331 -- Dummy definitions for implementations lacking asynchonous exceptions
332
333 block   :: IO a -> IO a
334 block    = id
335 unblock :: IO a -> IO a
336 unblock  = id
337 blocked :: IO Bool
338 blocked  = return False
339 #endif
340
341 -----------------------------------------------------------------------------
342 -- Catching exceptions
343
344 -- |This is the simplest of the exception-catching functions.  It
345 -- takes a single argument, runs it, and if an exception is raised
346 -- the \"handler\" is executed, with the value of the exception passed as an
347 -- argument.  Otherwise, the result is returned as normal.  For example:
348 --
349 -- >   catch (readFile f)
350 -- >         (\e -> do let err = show (e :: IOException)
351 -- >                   hPutStr stderr ("Warning: Couldn't open " ++ f ++ ": " ++ err)
352 -- >                   return "")
353 --
354 -- Note that we have to give a type signature to @e@, or the program
355 -- will not typecheck as the type is ambiguous. While it is possible
356 -- to catch exceptions of any type, see the previous section \"Catching all
357 -- exceptions\" for an explanation of the problems with doing so.
358 --
359 -- For catching exceptions in pure (non-'IO') expressions, see the
360 -- function 'evaluate'.
361 --
362 -- Note that due to Haskell\'s unspecified evaluation order, an
363 -- expression may throw one of several possible exceptions: consider
364 -- the expression @(error \"urk\") + (1 \`div\` 0)@.  Does
365 -- the expression throw
366 -- @ErrorCall \"urk\"@, or @DivideByZero@?
367 --
368 -- The answer is \"it might throw either\"; the choice is
369 -- non-deterministic. If you are catching any type of exception then you
370 -- might catch either. If you are calling @catch@ with type
371 -- @IO Int -> (ArithException -> IO Int) -> IO Int@ then the handler may
372 -- get run with @DivideByZero@ as an argument, or an @ErrorCall \"urk\"@
373 -- exception may be propogated further up. If you call it again, you
374 -- might get a the opposite behaviour. This is ok, because 'catch' is an
375 -- 'IO' computation.
376 --
377 -- Note that the "Prelude" also exports a function called
378 -- 'Prelude.catch' with a similar type to 'Control.Exception.catch',
379 -- except that the "Prelude" version only catches the IO and user
380 -- families of exceptions (as required by Haskell 98).
381 --
382 -- We recommend either hiding the "Prelude" version of 'Prelude.catch'
383 -- when importing "Control.Exception":
384 --
385 -- > import Prelude hiding (catch)
386 --
387 -- or importing "Control.Exception" qualified, to avoid name-clashes:
388 --
389 -- > import qualified Control.Exception as C
390 --
391 -- and then using @C.catch@
392 --
393 #ifndef __NHC__
394 catch   :: Exception e
395         => IO a         -- ^ The computation to run
396         -> (e -> IO a)  -- ^ Handler to invoke if an exception is raised
397         -> IO a
398 #if __GLASGOW_HASKELL__
399 catch = GHC.IO.catchException
400 #elif __HUGS__
401 catch m h = Hugs.Exception.catchException m h'
402   where h' e = case fromException e of
403             Just e' -> h e'
404             Nothing -> throwIO e
405 #endif
406 #endif
407
408 -- | The function 'catchJust' is like 'catch', but it takes an extra
409 -- argument which is an /exception predicate/, a function which
410 -- selects which type of exceptions we\'re interested in.
411 --
412 -- > catchJust (\e -> if isDoesNotExistErrorType (ioeGetErrorType e) then Just () else Nothing)
413 -- >           (readFile f)
414 -- >           (\_ -> do hPutStrLn stderr ("No such file: " ++ show f)
415 -- >                     return "")
416 --
417 -- Any other exceptions which are not matched by the predicate
418 -- are re-raised, and may be caught by an enclosing
419 -- 'catch', 'catchJust', etc.
420 catchJust
421         :: Exception e
422         => (e -> Maybe b)         -- ^ Predicate to select exceptions
423         -> IO a                   -- ^ Computation to run
424         -> (b -> IO a)            -- ^ Handler
425         -> IO a
426 catchJust p a handler = catch a handler'
427   where handler' e = case p e of
428                         Nothing -> throw e
429                         Just b  -> handler b
430
431 -- | A version of 'catch' with the arguments swapped around; useful in
432 -- situations where the code for the handler is shorter.  For example:
433 --
434 -- >   do handle (\NonTermination -> exitWith (ExitFailure 1)) $
435 -- >      ...
436 handle     :: Exception e => (e -> IO a) -> IO a -> IO a
437 handle     =  flip catch
438
439 -- | A version of 'catchJust' with the arguments swapped around (see
440 -- 'handle').
441 handleJust :: Exception e => (e -> Maybe b) -> (b -> IO a) -> IO a -> IO a
442 handleJust p =  flip (catchJust p)
443
444 -----------------------------------------------------------------------------
445 -- 'mapException'
446
447 -- | This function maps one exception into another as proposed in the
448 -- paper \"A semantics for imprecise exceptions\".
449
450 -- Notice that the usage of 'unsafePerformIO' is safe here.
451
452 mapException :: (Exception e1, Exception e2) => (e1 -> e2) -> a -> a
453 mapException f v = unsafePerformIO (catch (evaluate v)
454                                           (\x -> throw (f x)))
455
456 -----------------------------------------------------------------------------
457 -- 'try' and variations.
458
459 -- | Similar to 'catch', but returns an 'Either' result which is
460 -- @('Right' a)@ if no exception of type @e@ was raised, or @('Left' ex)@
461 -- if an exception of type @e@ was raised and its value is @ex@.
462 -- If any other type of exception is raised than it will be propogated
463 -- up to the next enclosing exception handler.
464 --
465 -- >  try a = catch (Right `liftM` a) (return . Left)
466 --
467 -- Note that "System.IO.Error" also exports a function called
468 -- 'System.IO.Error.try' with a similar type to 'Control.Exception.try',
469 -- except that it catches only the IO and user families of exceptions
470 -- (as required by the Haskell 98 @IO@ module).
471
472 try :: Exception e => IO a -> IO (Either e a)
473 try a = catch (a >>= \ v -> return (Right v)) (\e -> return (Left e))
474
475 -- | A variant of 'try' that takes an exception predicate to select
476 -- which exceptions are caught (c.f. 'catchJust').  If the exception
477 -- does not match the predicate, it is re-thrown.
478 tryJust :: Exception e => (e -> Maybe b) -> IO a -> IO (Either b a)
479 tryJust p a = do
480   r <- try a
481   case r of
482         Right v -> return (Right v)
483         Left  e -> case p e of
484                         Nothing -> throw e
485                         Just b  -> return (Left b)
486
487 -- | Like 'finally', but only performs the final action if there was an
488 -- exception raised by the computation.
489 onException :: IO a -> IO b -> IO a
490 onException io what = io `catch` \e -> do _ <- what
491                                           throw (e :: SomeException)
492
493 -----------------------------------------------------------------------------
494 -- Some Useful Functions
495
496 -- | When you want to acquire a resource, do some work with it, and
497 -- then release the resource, it is a good idea to use 'bracket',
498 -- because 'bracket' will install the necessary exception handler to
499 -- release the resource in the event that an exception is raised
500 -- during the computation.  If an exception is raised, then 'bracket' will
501 -- re-raise the exception (after performing the release).
502 --
503 -- A common example is opening a file:
504 --
505 -- > bracket
506 -- >   (openFile "filename" ReadMode)
507 -- >   (hClose)
508 -- >   (\fileHandle -> do { ... })
509 --
510 -- The arguments to 'bracket' are in this order so that we can partially apply
511 -- it, e.g.:
512 --
513 -- > withFile name mode = bracket (openFile name mode) hClose
514 --
515 #ifndef __NHC__
516 bracket
517         :: IO a         -- ^ computation to run first (\"acquire resource\")
518         -> (a -> IO b)  -- ^ computation to run last (\"release resource\")
519         -> (a -> IO c)  -- ^ computation to run in-between
520         -> IO c         -- returns the value from the in-between computation
521 bracket before after thing =
522   mask $ \restore -> do
523     a <- before
524     r <- restore (thing a) `onException` after a
525     _ <- after a
526     return r
527 #endif
528
529 -- | A specialised variant of 'bracket' with just a computation to run
530 -- afterward.
531 --
532 finally :: IO a         -- ^ computation to run first
533         -> IO b         -- ^ computation to run afterward (even if an exception
534                         -- was raised)
535         -> IO a         -- returns the value from the first computation
536 a `finally` sequel =
537   mask $ \restore -> do
538     r <- restore a `onException` sequel
539     _ <- sequel
540     return r
541
542 -- | A variant of 'bracket' where the return value from the first computation
543 -- is not required.
544 bracket_ :: IO a -> IO b -> IO c -> IO c
545 bracket_ before after thing = bracket before (const after) (const thing)
546
547 -- | Like 'bracket', but only performs the final action if there was an
548 -- exception raised by the in-between computation.
549 bracketOnError
550         :: IO a         -- ^ computation to run first (\"acquire resource\")
551         -> (a -> IO b)  -- ^ computation to run last (\"release resource\")
552         -> (a -> IO c)  -- ^ computation to run in-between
553         -> IO c         -- returns the value from the in-between computation
554 bracketOnError before after thing =
555   mask $ \restore -> do
556     a <- before
557     restore (thing a) `onException` after a
558
559 #if !(__GLASGOW_HASKELL__ || __NHC__)
560 assert :: Bool -> a -> a
561 assert True x = x
562 assert False _ = throw (AssertionFailed "")
563 #endif
564
565 -----
566
567 #if __GLASGOW_HASKELL__ || __HUGS__
568 -- |A pattern match failed. The @String@ gives information about the
569 -- source location of the pattern.
570 data PatternMatchFail = PatternMatchFail String
571 INSTANCE_TYPEABLE0(PatternMatchFail,patternMatchFailTc,"PatternMatchFail")
572
573 instance Show PatternMatchFail where
574     showsPrec _ (PatternMatchFail err) = showString err
575
576 #ifdef __HUGS__
577 instance Exception PatternMatchFail where
578     toException (PatternMatchFail err) = Hugs.Exception.PatternMatchFail err
579     fromException (Hugs.Exception.PatternMatchFail err) = Just (PatternMatchFail err)
580     fromException _ = Nothing
581 #else
582 instance Exception PatternMatchFail
583 #endif
584
585 -----
586
587 -- |A record selector was applied to a constructor without the
588 -- appropriate field. This can only happen with a datatype with
589 -- multiple constructors, where some fields are in one constructor
590 -- but not another. The @String@ gives information about the source
591 -- location of the record selector.
592 data RecSelError = RecSelError String
593 INSTANCE_TYPEABLE0(RecSelError,recSelErrorTc,"RecSelError")
594
595 instance Show RecSelError where
596     showsPrec _ (RecSelError err) = showString err
597
598 #ifdef __HUGS__
599 instance Exception RecSelError where
600     toException (RecSelError err) = Hugs.Exception.RecSelError err
601     fromException (Hugs.Exception.RecSelError err) = Just (RecSelError err)
602     fromException _ = Nothing
603 #else
604 instance Exception RecSelError
605 #endif
606
607 -----
608
609 -- |An uninitialised record field was used. The @String@ gives
610 -- information about the source location where the record was
611 -- constructed.
612 data RecConError = RecConError String
613 INSTANCE_TYPEABLE0(RecConError,recConErrorTc,"RecConError")
614
615 instance Show RecConError where
616     showsPrec _ (RecConError err) = showString err
617
618 #ifdef __HUGS__
619 instance Exception RecConError where
620     toException (RecConError err) = Hugs.Exception.RecConError err
621     fromException (Hugs.Exception.RecConError err) = Just (RecConError err)
622     fromException _ = Nothing
623 #else
624 instance Exception RecConError
625 #endif
626
627 -----
628
629 -- |A record update was performed on a constructor without the
630 -- appropriate field. This can only happen with a datatype with
631 -- multiple constructors, where some fields are in one constructor
632 -- but not another. The @String@ gives information about the source
633 -- location of the record update.
634 data RecUpdError = RecUpdError String
635 INSTANCE_TYPEABLE0(RecUpdError,recUpdErrorTc,"RecUpdError")
636
637 instance Show RecUpdError where
638     showsPrec _ (RecUpdError err) = showString err
639
640 #ifdef __HUGS__
641 instance Exception RecUpdError where
642     toException (RecUpdError err) = Hugs.Exception.RecUpdError err
643     fromException (Hugs.Exception.RecUpdError err) = Just (RecUpdError err)
644     fromException _ = Nothing
645 #else
646 instance Exception RecUpdError
647 #endif
648
649 -----
650
651 -- |A class method without a definition (neither a default definition,
652 -- nor a definition in the appropriate instance) was called. The
653 -- @String@ gives information about which method it was.
654 data NoMethodError = NoMethodError String
655 INSTANCE_TYPEABLE0(NoMethodError,noMethodErrorTc,"NoMethodError")
656
657 instance Show NoMethodError where
658     showsPrec _ (NoMethodError err) = showString err
659
660 #ifdef __HUGS__
661 instance Exception NoMethodError where
662     toException (NoMethodError err) = Hugs.Exception.NoMethodError err
663     fromException (Hugs.Exception.NoMethodError err) = Just (NoMethodError err)
664     fromException _ = Nothing
665 #else
666 instance Exception NoMethodError
667 #endif
668
669 -----
670
671 -- |Thrown when the runtime system detects that the computation is
672 -- guaranteed not to terminate. Note that there is no guarantee that
673 -- the runtime system will notice whether any given computation is
674 -- guaranteed to terminate or not.
675 data NonTermination = NonTermination
676 INSTANCE_TYPEABLE0(NonTermination,nonTerminationTc,"NonTermination")
677
678 instance Show NonTermination where
679     showsPrec _ NonTermination = showString "<<loop>>"
680
681 #ifdef __HUGS__
682 instance Exception NonTermination where
683     toException NonTermination = Hugs.Exception.NonTermination
684     fromException Hugs.Exception.NonTermination = Just NonTermination
685     fromException _ = Nothing
686 #else
687 instance Exception NonTermination
688 #endif
689
690 -----
691
692 -- |Thrown when the program attempts to call @atomically@, from the @stm@
693 -- package, inside another call to @atomically@.
694 data NestedAtomically = NestedAtomically
695 INSTANCE_TYPEABLE0(NestedAtomically,nestedAtomicallyTc,"NestedAtomically")
696
697 instance Show NestedAtomically where
698     showsPrec _ NestedAtomically = showString "Control.Concurrent.STM.atomically was nested"
699
700 instance Exception NestedAtomically
701
702 -----
703
704 #endif /* __GLASGOW_HASKELL__ || __HUGS__ */
705
706 #ifdef __GLASGOW_HASKELL__
707 recSelError, recConError, irrefutPatError, runtimeError,
708              nonExhaustiveGuardsError, patError, noMethodBindingError
709         :: Addr# -> a   -- All take a UTF8-encoded C string
710
711 recSelError              s = throw (RecSelError ("No match in record selector "
712                                                  ++ unpackCStringUtf8# s))  -- No location info unfortunately
713 runtimeError             s = error (unpackCStringUtf8# s)                   -- No location info unfortunately
714
715 nonExhaustiveGuardsError s = throw (PatternMatchFail (untangle s "Non-exhaustive guards in"))
716 irrefutPatError          s = throw (PatternMatchFail (untangle s "Irrefutable pattern failed for pattern"))
717 recConError              s = throw (RecConError      (untangle s "Missing field in record construction"))
718 noMethodBindingError     s = throw (NoMethodError    (untangle s "No instance nor default method for class operation"))
719 patError                 s = throw (PatternMatchFail (untangle s "Non-exhaustive patterns in"))
720
721 -- GHC's RTS calls this
722 nonTermination :: SomeException
723 nonTermination = toException NonTermination
724
725 -- GHC's RTS calls this
726 nestedAtomically :: SomeException
727 nestedAtomically = toException NestedAtomically
728 #endif