Re-add blocked; it got lost in the extensible exceptions patches
[ghc-base.git] / Control / Exception.hs
1 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  Control.Exception
5 -- Copyright   :  (c) The University of Glasgow 2001
6 -- License     :  BSD-style (see the file libraries/base/LICENSE)
7 -- 
8 -- Maintainer  :  libraries@haskell.org
9 -- Stability   :  experimental
10 -- Portability :  non-portable (extended exceptions)
11 --
12 -- This module provides support for raising and catching both built-in
13 -- and user-defined exceptions.
14 --
15 -- In addition to exceptions thrown by 'IO' operations, exceptions may
16 -- be thrown by pure code (imprecise exceptions) or by external events
17 -- (asynchronous exceptions), but may only be caught in the 'IO' monad.
18 -- For more details, see:
19 --
20 --  * /A semantics for imprecise exceptions/, by Simon Peyton Jones,
21 --    Alastair Reid, Tony Hoare, Simon Marlow, Fergus Henderson,
22 --    in /PLDI'99/.
23 --
24 --  * /Asynchronous exceptions in Haskell/, by Simon Marlow, Simon Peyton
25 --    Jones, Andy Moran and John Reppy, in /PLDI'01/.
26 --
27 -----------------------------------------------------------------------------
28
29 module Control.Exception (
30
31         -- * The Exception type
32         SomeException(..),
33         Exception(..),          -- instance Eq, Ord, Show, Typeable
34         IOException,            -- instance Eq, Ord, Show, Typeable
35         ArithException(..),     -- instance Eq, Ord, Show, Typeable
36         ArrayException(..),     -- instance Eq, Ord, Show, Typeable
37         AssertionFailed(..),
38         AsyncException(..),     -- instance Eq, Ord, Show, Typeable
39         NonTermination(..), nonTermination,
40         BlockedOnDeadMVar(..),
41         BlockedIndefinitely(..),
42         NestedAtomically(..), nestedAtomically,
43         Deadlock(..),
44         NoMethodError(..),
45         PatternMatchFail(..),
46         RecConError(..),
47         RecSelError(..),
48         RecUpdError(..),
49
50         -- * Throwing exceptions
51         throwIO,        -- :: Exception -> IO a
52         throw,          -- :: Exception -> a
53         ioError,        -- :: IOError -> IO a
54 #ifdef __GLASGOW_HASKELL__
55         throwTo,        -- :: ThreadId -> Exception -> a
56 #endif
57
58         -- * Catching Exceptions
59
60         -- |There are several functions for catching and examining
61         -- exceptions; all of them may only be used from within the
62         -- 'IO' monad.
63
64         -- ** The @catch@ functions
65         catch,     -- :: IO a -> (Exception -> IO a) -> IO a
66         catches, Handler(..),
67         catchAny,
68         catchJust, -- :: (Exception -> Maybe b) -> IO a -> (b -> IO a) -> IO a
69
70         -- ** The @handle@ functions
71         handle,    -- :: (Exception -> IO a) -> IO a -> IO a
72         handleAny,
73         handleJust,-- :: (Exception -> Maybe b) -> (b -> IO a) -> IO a -> IO a
74
75         -- ** The @try@ functions
76         try,       -- :: IO a -> IO (Either Exception a)
77         tryJust,   -- :: (Exception -> Maybe b) -> a    -> IO (Either b a)
78         ignoreExceptions,
79
80         -- ** The @evaluate@ function
81         evaluate,  -- :: a -> IO a
82
83         -- ** The @mapException@ function
84         mapException,           -- :: (Exception -> Exception) -> a -> a
85
86         -- * Asynchronous Exceptions
87
88         -- $async
89
90         -- ** Asynchronous exception control
91
92         -- |The following two functions allow a thread to control delivery of
93         -- asynchronous exceptions during a critical region.
94
95         block,          -- :: IO a -> IO a
96         unblock,        -- :: IO a -> IO a
97         blocked,        -- :: IO Bool
98
99         -- *** Applying @block@ to an exception handler
100
101         -- $block_handler
102
103         -- *** Interruptible operations
104
105         -- $interruptible
106
107         -- * Assertions
108
109         assert,         -- :: Bool -> a -> a
110
111         -- * Utilities
112
113         bracket,        -- :: IO a -> (a -> IO b) -> (a -> IO c) -> IO ()
114         bracket_,       -- :: IO a -> IO b -> IO c -> IO ()
115         bracketOnError,
116
117         finally,        -- :: IO a -> IO b -> IO a
118
119         recSelError, recConError, irrefutPatError, runtimeError,
120         nonExhaustiveGuardsError, patError, noMethodBindingError,
121
122 #ifdef __GLASGOW_HASKELL__
123         setUncaughtExceptionHandler,      -- :: (Exception -> IO ()) -> IO ()
124         getUncaughtExceptionHandler       -- :: IO (Exception -> IO ())
125 #endif
126   ) where
127
128 #ifdef __GLASGOW_HASKELL__
129 import GHC.Base
130 import GHC.IOBase
131 import {-# SOURCE #-} GHC.Handle
132 import GHC.List
133 import GHC.Num
134 import GHC.Show
135 import GHC.IOBase as ExceptionBase
136 import GHC.Exception hiding ( Exception )
137 import {-# SOURCE #-} GHC.Conc         ( ThreadId(ThreadId) )
138 import Foreign.C.String ( CString, withCString )
139 #endif
140
141 #ifdef __HUGS__
142 import Hugs.Exception   as ExceptionBase
143 #endif
144
145 import Data.Dynamic
146 import Data.Either
147 import Data.Maybe
148
149 #ifdef __NHC__
150 import qualified System.IO.Error as H'98 (catch)
151 import System.IO.Error (ioError)
152 import IO              (bracket)
153 import DIOError         -- defn of IOError type
154 import System          (ExitCode())
155
156 -- minimum needed for nhc98 to pretend it has Exceptions
157 data Exception   = IOException    IOException
158                  | ArithException ArithException
159                  | ArrayException ArrayException
160                  | AsyncException AsyncException
161                  | ExitException  ExitCode
162                  deriving Show
163 type IOException = IOError
164 data ArithException
165 data ArrayException
166 data AsyncException
167 instance Show ArithException
168 instance Show ArrayException
169 instance Show AsyncException
170
171 catch    :: IO a -> (Exception -> IO a) -> IO a
172 a `catch` b = a `H'98.catch` (b . IOException)
173
174 throwIO  :: Exception -> IO a
175 throwIO (IOException e) = ioError e
176 throwIO _               = ioError (UserError "Control.Exception.throwIO"
177                                              "unknown exception")
178 throw    :: Exception -> a
179 throw     = unsafePerformIO . throwIO
180
181 evaluate :: a -> IO a
182 evaluate x = x `seq` return x
183
184 assert :: Bool -> a -> a
185 assert True  x = x
186 assert False _ = throw (IOException (UserError "" "Assertion failed"))
187 #endif
188
189 #ifndef __GLASGOW_HASKELL__
190 -- Dummy definitions for implementations lacking asynchonous exceptions
191
192 block   :: IO a -> IO a
193 block    = id
194 unblock :: IO a -> IO a
195 unblock  = id
196 blocked :: IO Bool
197 blocked  = return False
198 #endif
199
200 -----------------------------------------------------------------------------
201 -- Catching exceptions
202
203 -- |This is the simplest of the exception-catching functions.  It
204 -- takes a single argument, runs it, and if an exception is raised
205 -- the \"handler\" is executed, with the value of the exception passed as an
206 -- argument.  Otherwise, the result is returned as normal.  For example:
207 --
208 -- >   catch (openFile f ReadMode) 
209 -- >       (\e -> hPutStr stderr ("Couldn't open "++f++": " ++ show e))
210 --
211 -- For catching exceptions in pure (non-'IO') expressions, see the
212 -- function 'evaluate'.
213 --
214 -- Note that due to Haskell\'s unspecified evaluation order, an
215 -- expression may return one of several possible exceptions: consider
216 -- the expression @error \"urk\" + 1 \`div\` 0@.  Does
217 -- 'catch' execute the handler passing
218 -- @ErrorCall \"urk\"@, or @ArithError DivideByZero@?
219 --
220 -- The answer is \"either\": 'catch' makes a
221 -- non-deterministic choice about which exception to catch.  If you
222 -- call it again, you might get a different exception back.  This is
223 -- ok, because 'catch' is an 'IO' computation.
224 --
225 -- Note that 'catch' catches all types of exceptions, and is generally
226 -- used for \"cleaning up\" before passing on the exception using
227 -- 'throwIO'.  It is not good practice to discard the exception and
228 -- continue, without first checking the type of the exception (it
229 -- might be a 'ThreadKilled', for example).  In this case it is usually better
230 -- to use 'catchJust' and select the kinds of exceptions to catch.
231 --
232 -- Also note that the "Prelude" also exports a function called
233 -- 'Prelude.catch' with a similar type to 'Control.Exception.catch',
234 -- except that the "Prelude" version only catches the IO and user
235 -- families of exceptions (as required by Haskell 98).  
236 --
237 -- We recommend either hiding the "Prelude" version of 'Prelude.catch'
238 -- when importing "Control.Exception": 
239 --
240 -- > import Prelude hiding (catch)
241 --
242 -- or importing "Control.Exception" qualified, to avoid name-clashes:
243 --
244 -- > import qualified Control.Exception as C
245 --
246 -- and then using @C.catch@
247 --
248 #ifndef __NHC__
249 catch   :: Exception e
250         => IO a         -- ^ The computation to run
251         -> (e -> IO a)  -- ^ Handler to invoke if an exception is raised
252         -> IO a
253 catch = ExceptionBase.catchException
254
255 catches :: IO a -> [Handler a] -> IO a
256 catches io handlers = io `catch` catchesHandler handlers
257
258 catchesHandler :: [Handler a] -> SomeException -> IO a
259 catchesHandler handlers e = foldr tryHandler (throw e) handlers
260     where tryHandler (Handler handler) res
261               = case fromException e of
262                 Just e' -> handler e'
263                 Nothing -> res
264
265 data Handler a = forall e . Exception e => Handler (e -> IO a)
266 #endif
267 -- | The function 'catchJust' is like 'catch', but it takes an extra
268 -- argument which is an /exception predicate/, a function which
269 -- selects which type of exceptions we\'re interested in.
270 --
271 -- >   result <- catchJust errorCalls thing_to_try handler
272 --
273 -- Any other exceptions which are not matched by the predicate
274 -- are re-raised, and may be caught by an enclosing
275 -- 'catch' or 'catchJust'.
276 catchJust
277         :: Exception e
278         => (e -> Maybe b)         -- ^ Predicate to select exceptions
279         -> IO a                   -- ^ Computation to run
280         -> (b -> IO a)            -- ^ Handler
281         -> IO a
282 catchJust p a handler = catch a handler'
283   where handler' e = case p e of 
284                         Nothing -> throw e
285                         Just b  -> handler b
286
287 -- | A version of 'catch' with the arguments swapped around; useful in
288 -- situations where the code for the handler is shorter.  For example:
289 --
290 -- >   do handle (\e -> exitWith (ExitFailure 1)) $
291 -- >      ...
292 handle     :: Exception e => (e -> IO a) -> IO a -> IO a
293 handle     =  flip catch
294
295 handleAny  :: (forall e . Exception e => e -> IO a) -> IO a -> IO a
296 handleAny  =  flip catchAny
297
298 -- | A version of 'catchJust' with the arguments swapped around (see
299 -- 'handle').
300 handleJust :: Exception e => (e -> Maybe b) -> (b -> IO a) -> IO a -> IO a
301 handleJust p =  flip (catchJust p)
302
303 -----------------------------------------------------------------------------
304 -- 'mapException'
305
306 -- | This function maps one exception into another as proposed in the
307 -- paper \"A semantics for imprecise exceptions\".
308
309 -- Notice that the usage of 'unsafePerformIO' is safe here.
310
311 mapException :: Exception e => (e -> e) -> a -> a
312 mapException f v = unsafePerformIO (catch (evaluate v)
313                                           (\x -> throw (f x)))
314
315 -----------------------------------------------------------------------------
316 -- 'try' and variations.
317
318 -- | Similar to 'catch', but returns an 'Either' result which is
319 -- @('Right' a)@ if no exception was raised, or @('Left' e)@ if an
320 -- exception was raised and its value is @e@.
321 --
322 -- >  try a = catch (Right `liftM` a) (return . Left)
323 --
324 -- Note: as with 'catch', it is only polite to use this variant if you intend
325 -- to re-throw the exception after performing whatever cleanup is needed.
326 -- Otherwise, 'tryJust' is generally considered to be better.
327 --
328 -- Also note that "System.IO.Error" also exports a function called
329 -- 'System.IO.Error.try' with a similar type to 'Control.Exception.try',
330 -- except that it catches only the IO and user families of exceptions
331 -- (as required by the Haskell 98 @IO@ module).
332
333 try :: Exception e => IO a -> IO (Either e a)
334 try a = catch (a >>= \ v -> return (Right v)) (\e -> return (Left e))
335
336 -- | A variant of 'try' that takes an exception predicate to select
337 -- which exceptions are caught (c.f. 'catchJust').  If the exception
338 -- does not match the predicate, it is re-thrown.
339 tryJust :: Exception e => (e -> Maybe b) -> IO a -> IO (Either b a)
340 tryJust p a = do
341   r <- try a
342   case r of
343         Right v -> return (Right v)
344         Left  e -> case p e of
345                         Nothing -> throw e
346                         Just b  -> return (Left b)
347
348 ignoreExceptions :: IO () -> IO ()
349 ignoreExceptions io = io `catchAny` \_ -> return ()
350
351 -----------------------------------------------------------------------------
352 -- Some Useful Functions
353
354 -- | When you want to acquire a resource, do some work with it, and
355 -- then release the resource, it is a good idea to use 'bracket',
356 -- because 'bracket' will install the necessary exception handler to
357 -- release the resource in the event that an exception is raised
358 -- during the computation.  If an exception is raised, then 'bracket' will 
359 -- re-raise the exception (after performing the release).
360 --
361 -- A common example is opening a file:
362 --
363 -- > bracket
364 -- >   (openFile "filename" ReadMode)
365 -- >   (hClose)
366 -- >   (\handle -> do { ... })
367 --
368 -- The arguments to 'bracket' are in this order so that we can partially apply 
369 -- it, e.g.:
370 --
371 -- > withFile name mode = bracket (openFile name mode) hClose
372 --
373 #ifndef __NHC__
374 bracket 
375         :: IO a         -- ^ computation to run first (\"acquire resource\")
376         -> (a -> IO b)  -- ^ computation to run last (\"release resource\")
377         -> (a -> IO c)  -- ^ computation to run in-between
378         -> IO c         -- returns the value from the in-between computation
379 bracket before after thing =
380   block (do
381     a <- before 
382     r <- catchAny
383            (unblock (thing a))
384            (\e -> do { after a; throw e })
385     after a
386     return r
387  )
388 #endif
389
390 -- | A specialised variant of 'bracket' with just a computation to run
391 -- afterward.
392 -- 
393 finally :: IO a         -- ^ computation to run first
394         -> IO b         -- ^ computation to run afterward (even if an exception 
395                         -- was raised)
396         -> IO a         -- returns the value from the first computation
397 a `finally` sequel =
398   block (do
399     r <- catchAny
400              (unblock a)
401              (\e -> do { sequel; throw e })
402     sequel
403     return r
404   )
405
406 -- | A variant of 'bracket' where the return value from the first computation
407 -- is not required.
408 bracket_ :: IO a -> IO b -> IO c -> IO c
409 bracket_ before after thing = bracket before (const after) (const thing)
410
411 -- | Like bracket, but only performs the final action if there was an 
412 -- exception raised by the in-between computation.
413 bracketOnError
414         :: IO a         -- ^ computation to run first (\"acquire resource\")
415         -> (a -> IO b)  -- ^ computation to run last (\"release resource\")
416         -> (a -> IO c)  -- ^ computation to run in-between
417         -> IO c         -- returns the value from the in-between computation
418 bracketOnError before after thing =
419   block (do
420     a <- before 
421     catchAny
422         (unblock (thing a))
423         (\e -> do { after a; throw e })
424  )
425
426 -- -----------------------------------------------------------------------------
427 -- Asynchronous exceptions
428
429 {- $async
430
431  #AsynchronousExceptions# Asynchronous exceptions are so-called because they arise due to
432 external influences, and can be raised at any point during execution.
433 'StackOverflow' and 'HeapOverflow' are two examples of
434 system-generated asynchronous exceptions.
435
436 The primary source of asynchronous exceptions, however, is
437 'throwTo':
438
439 >  throwTo :: ThreadId -> Exception -> IO ()
440
441 'throwTo' (also 'throwDynTo' and 'Control.Concurrent.killThread') allows one
442 running thread to raise an arbitrary exception in another thread.  The
443 exception is therefore asynchronous with respect to the target thread,
444 which could be doing anything at the time it receives the exception.
445 Great care should be taken with asynchronous exceptions; it is all too
446 easy to introduce race conditions by the over zealous use of
447 'throwTo'.
448 -}
449
450 {- $block_handler
451 There\'s an implied 'block' around every exception handler in a call
452 to one of the 'catch' family of functions.  This is because that is
453 what you want most of the time - it eliminates a common race condition
454 in starting an exception handler, because there may be no exception
455 handler on the stack to handle another exception if one arrives
456 immediately.  If asynchronous exceptions are blocked on entering the
457 handler, though, we have time to install a new exception handler
458 before being interrupted.  If this weren\'t the default, one would have
459 to write something like
460
461 >      block (
462 >           catch (unblock (...))
463 >                      (\e -> handler)
464 >      )
465
466 If you need to unblock asynchronous exceptions again in the exception
467 handler, just use 'unblock' as normal.
468
469 Note that 'try' and friends /do not/ have a similar default, because
470 there is no exception handler in this case.  If you want to use 'try'
471 in an asynchronous-exception-safe way, you will need to use
472 'block'.
473 -}
474
475 {- $interruptible
476
477 Some operations are /interruptible/, which means that they can receive
478 asynchronous exceptions even in the scope of a 'block'.  Any function
479 which may itself block is defined as interruptible; this includes
480 'Control.Concurrent.MVar.takeMVar'
481 (but not 'Control.Concurrent.MVar.tryTakeMVar'),
482 and most operations which perform
483 some I\/O with the outside world.  The reason for having
484 interruptible operations is so that we can write things like
485
486 >      block (
487 >         a <- takeMVar m
488 >         catch (unblock (...))
489 >               (\e -> ...)
490 >      )
491
492 if the 'Control.Concurrent.MVar.takeMVar' was not interruptible,
493 then this particular
494 combination could lead to deadlock, because the thread itself would be
495 blocked in a state where it can\'t receive any asynchronous exceptions.
496 With 'Control.Concurrent.MVar.takeMVar' interruptible, however, we can be
497 safe in the knowledge that the thread can receive exceptions right up
498 until the point when the 'Control.Concurrent.MVar.takeMVar' succeeds.
499 Similar arguments apply for other interruptible operations like
500 'System.IO.openFile'.
501 -}
502
503 #if !(__GLASGOW_HASKELL__ || __NHC__)
504 assert :: Bool -> a -> a
505 assert True x = x
506 assert False _ = throw (AssertionFailed "")
507 #endif
508
509
510 #ifdef __GLASGOW_HASKELL__
511 {-# NOINLINE uncaughtExceptionHandler #-}
512 uncaughtExceptionHandler :: IORef (SomeException -> IO ())
513 uncaughtExceptionHandler = unsafePerformIO (newIORef defaultHandler)
514    where
515       defaultHandler :: SomeException -> IO ()
516       defaultHandler se@(SomeException ex) = do
517          (hFlush stdout) `catchAny` (\ _ -> return ())
518          let msg = case cast ex of
519                Just Deadlock -> "no threads to run:  infinite loop or deadlock?"
520                _ -> case cast ex of
521                     Just (ErrorCall s) -> s
522                     _                  -> showsPrec 0 se ""
523          withCString "%s" $ \cfmt ->
524           withCString msg $ \cmsg ->
525             errorBelch cfmt cmsg
526
527 -- don't use errorBelch() directly, because we cannot call varargs functions
528 -- using the FFI.
529 foreign import ccall unsafe "HsBase.h errorBelch2"
530    errorBelch :: CString -> CString -> IO ()
531
532 setUncaughtExceptionHandler :: (SomeException -> IO ()) -> IO ()
533 setUncaughtExceptionHandler = writeIORef uncaughtExceptionHandler
534
535 getUncaughtExceptionHandler :: IO (SomeException -> IO ())
536 getUncaughtExceptionHandler = readIORef uncaughtExceptionHandler
537 #endif
538
539 recSelError, recConError, irrefutPatError, runtimeError,
540              nonExhaustiveGuardsError, patError, noMethodBindingError
541         :: Addr# -> a   -- All take a UTF8-encoded C string
542
543 recSelError              s = throw (RecSelError (unpackCStringUtf8# s)) -- No location info unfortunately
544 runtimeError             s = error (unpackCStringUtf8# s)               -- No location info unfortunately
545
546 nonExhaustiveGuardsError s = throw (PatternMatchFail (untangle s "Non-exhaustive guards in"))
547 irrefutPatError          s = throw (PatternMatchFail (untangle s "Irrefutable pattern failed for pattern"))
548 recConError              s = throw (RecConError      (untangle s "Missing field in record construction"))
549 noMethodBindingError     s = throw (NoMethodError    (untangle s "No instance nor default method for class operation"))
550 patError                 s = throw (PatternMatchFail (untangle s "Non-exhaustive patterns in"))
551
552 -----
553
554 data PatternMatchFail = PatternMatchFail String
555     deriving Typeable
556
557 instance Exception PatternMatchFail
558
559 instance Show PatternMatchFail where
560     showsPrec _ (PatternMatchFail err) = showString err
561
562 -----
563
564 data RecSelError = RecSelError String
565     deriving Typeable
566
567 instance Exception RecSelError
568
569 instance Show RecSelError where
570     showsPrec _ (RecSelError err) = showString err
571
572 -----
573
574 data RecConError = RecConError String
575     deriving Typeable
576
577 instance Exception RecConError
578
579 instance Show RecConError where
580     showsPrec _ (RecConError err) = showString err
581
582 -----
583
584 data RecUpdError = RecUpdError String
585     deriving Typeable
586
587 instance Exception RecUpdError
588
589 instance Show RecUpdError where
590     showsPrec _ (RecUpdError err) = showString err
591
592 -----
593
594 data NoMethodError = NoMethodError String
595     deriving Typeable
596
597 instance Exception NoMethodError
598
599 instance Show NoMethodError where
600     showsPrec _ (NoMethodError err) = showString err
601
602 -----
603
604 data AssertionFailed = AssertionFailed String
605     deriving Typeable
606
607 instance Exception AssertionFailed
608
609 instance Show AssertionFailed where
610     showsPrec _ (AssertionFailed err) = showString err
611
612 -----
613
614 data NonTermination = NonTermination
615     deriving Typeable
616
617 instance Exception NonTermination
618
619 instance Show NonTermination where
620     showsPrec _ NonTermination = showString "<<loop>>"
621
622 -- GHC's RTS calls this
623 nonTermination :: SomeException
624 nonTermination = toException NonTermination
625
626 -----
627
628 data Deadlock = Deadlock
629     deriving Typeable
630
631 instance Exception Deadlock
632
633 instance Show Deadlock where
634     showsPrec _ Deadlock = showString "<<deadlock>>"
635
636 -----
637
638 data NestedAtomically = NestedAtomically
639     deriving Typeable
640
641 instance Exception NestedAtomically
642
643 instance Show NestedAtomically where
644     showsPrec _ NestedAtomically = showString "Control.Concurrent.STM.atomically was nested"
645
646 -- GHC's RTS calls this
647 nestedAtomically :: SomeException
648 nestedAtomically = toException NestedAtomically
649
650 -----
651
652 instance Exception Dynamic
653
654 -----
655
656 assertError :: Addr# -> Bool -> a -> a
657 assertError str pred v
658   | pred      = v
659   | otherwise = throw (AssertionFailed (untangle str "Assertion failed"))
660
661 {-
662 (untangle coded message) expects "coded" to be of the form
663         "location|details"
664 It prints
665         location message details
666 -}
667 untangle :: Addr# -> String -> String
668 untangle coded message
669   =  location
670   ++ ": " 
671   ++ message
672   ++ details
673   ++ "\n"
674   where
675     coded_str = unpackCStringUtf8# coded
676
677     (location, details)
678       = case (span not_bar coded_str) of { (loc, rest) ->
679         case rest of
680           ('|':det) -> (loc, ' ' : det)
681           _         -> (loc, "")
682         }
683     not_bar c = c /= '|'
684
685 -- XXX From GHC.Conc
686 throwTo :: Exception e => ThreadId -> e -> IO ()
687 throwTo (ThreadId id) ex = IO $ \ s ->
688    case (killThread# id (toException ex) s) of s1 -> (# s1, () #)
689