2 -----------------------------------------------------------------------------
4 -- Module : GHC.TopHandler
5 -- Copyright : (c) The University of Glasgow, 2001-2002
6 -- License : see libraries/base/LICENSE
8 -- Maintainer : cvs-ghc@haskell.org
9 -- Stability : internal
10 -- Portability : non-portable (GHC Extensions)
12 -- Support for catching exceptions raised during top-level computations
13 -- (e.g. @Main.main@, 'Control.Concurrent.forkIO', and foreign exports)
15 -----------------------------------------------------------------------------
18 module GHC.TopHandler (
19 runMainIO, runIO, runNonIO, reportStackOverflow, reportError
25 import Control.Exception
29 import GHC.Prim (unsafeCoerce#)
31 -- | 'runMainIO' is wrapped around 'Main.main' (or whatever main is
32 -- called in the program). It catches otherwise uncaught exceptions,
33 -- and also flushes stdout\/stderr before exiting.
34 runMainIO :: IO a -> IO a
35 runMainIO main = (do a <- main; cleanUp; return a) `catchException` topHandler
37 -- | 'runIO' is wrapped around every @foreign export@ and @foreign
38 -- import \"wrapper\"@ to mop up any uncaught exceptions. Thus, the
39 -- result of running 'System.Exit.exitWith' in a foreign-exported
40 -- function is the same as in the main thread: it terminates the
44 runIO main = catchException main topHandler
46 -- | The same as 'runIO', but for non-IO computations. Used for
47 -- wrapping @foreign export@ and @foreign import \"wrapper\"@ when these
48 -- are used to export Haskell functions with non-IO types.
51 runNonIO a = catchException (a `seq` return a) topHandler
53 topHandler :: Exception -> IO a
54 topHandler err = catchException (real_handler err) topHandler
56 -- Make sure we handle errors while reporting the error!
57 -- (e.g. evaluating the string passed to 'error' might generate
58 -- another error, etc.)
60 real_handler :: Exception -> IO a
64 AsyncException StackOverflow -> do
68 -- only the main thread gets ExitException exceptions
69 ExitException ExitSuccess -> safeExit 0
70 ExitException (ExitFailure n) -> safeExit n
77 reportStackOverflow :: IO a
78 reportStackOverflow = do callStackOverflowHook; return undefined
80 reportError :: Exception -> IO a
82 handler <- getUncaughtExceptionHandler
86 -- SUP: Are the hooks allowed to re-enter Haskell land? If so, remove
88 foreign import ccall unsafe "stackOverflow"
89 callStackOverflowHook :: IO ()
91 -- try to flush stdout/stderr, but don't worry if we fail
92 -- (these handles might have errors, and we don't want to go into
96 hFlush stdout `catchException` \_ -> return ()
97 hFlush stderr `catchException` \_ -> return ()
99 cleanUpAndExit :: Int -> IO a
100 cleanUpAndExit r = do cleanUp; safeExit r
102 -- we have to use unsafeCoerce# to get the 'IO a' result type, since the
103 -- compiler doesn't let us declare that as the result type of a foreign export.
104 safeExit :: Int -> IO a
105 safeExit r = unsafeCoerce# (shutdownHaskellAndExit r)
107 -- NOTE: shutdownHaskellAndExit must be called "safe", because it *can*
108 -- re-enter Haskell land through finalizers.
109 foreign import ccall "shutdownHaskellAndExit"
110 shutdownHaskellAndExit :: Int -> IO ()