[project @ 2004-06-25 10:42:48 by ross]
[ghc-base.git] / GHC / TopHandler.lhs
index 691af14..8c123a2 100644 (file)
@@ -9,19 +9,19 @@
 -- Stability   :  internal
 -- Portability :  non-portable (GHC Extensions)
 --
--- Top-level IO actions want to catch exceptions (e.g., 'forkIO' and 
--- 'GHC.Main.mainIO') and report them - 'topHandler' is the exception
--- handler they should use for this.
+-- Support for catching exceptions raised during top-level computations
+-- (e.g. @Main.main@, 'Control.Concurrent.forkIO', and foreign exports)
 --
 -----------------------------------------------------------------------------
 
 module GHC.TopHandler (
-   runIO, runNonIO, reportStackOverflow, reportError 
+   runIO, runNonIO, reportStackOverflow, reportError
   ) where
 
 import Prelude
 
 import System.IO
+import Control.Exception
 
 import Foreign.C.String
 import Foreign.Ptr
@@ -29,7 +29,7 @@ import GHC.IOBase
 import GHC.Exception
 import GHC.Prim (unsafeCoerce#)
 
--- | 'runIO' is wrapped around 'Main.main' by @TcModule@.  It is also wrapped
+-- | 'runIO' is wrapped around @Main.main@ by @TcModule@.  It is also wrapped
 -- around every @foreign export@ and @foreign import \"wrapper\"@ to mop up
 -- any uncaught exceptions.  Thus, the result of running
 -- 'System.Exit.exitWith' in a foreign-exported function is the same as
@@ -61,11 +61,8 @@ real_handler ex =
        ExitException ExitSuccess     -> safe_exit 0
        ExitException (ExitFailure n) -> safe_exit n
 
-       Deadlock    -> reportError True 
-                       "no threads to run:  infinite loop or deadlock?"
-  
-       ErrorCall s -> reportError True s
-       other       -> reportError True (showsPrec 0 other "\n")
+       other       -> reportError True other
+          
 
 reportStackOverflow :: Bool -> IO a
 reportStackOverflow bombOut = do
@@ -75,23 +72,13 @@ reportStackOverflow bombOut = do
        then exit 2
        else return undefined
 
-reportError :: Bool -> String -> IO a
-reportError bombOut str = do
-   (hFlush stdout) `catchException` (\ _ -> return ())
-   withCStringLen str $ \(cstr,len) -> do
-     writeErrString errorHdrHook cstr len
-     if bombOut 
-       then exit 1
-        else return undefined
-
-#ifndef ILX
-foreign import ccall "&ErrorHdrHook" errorHdrHook :: Ptr ()
-#else
-foreign import ccall "ErrorHdrHook" errorHdrHook :: Ptr ()
-#endif
-
-foreign import ccall unsafe "writeErrString__"
-       writeErrString :: Ptr () -> CString -> Int -> IO ()
+reportError :: Bool -> Exception -> IO a
+reportError bombOut ex = do
+   handler <- getUncaughtExceptionHandler
+   handler ex
+   if bombOut
+      then exit 1
+      else return undefined
 
 -- SUP: Are the hooks allowed to re-enter Haskell land?  If so, remove
 -- the unsafe below.