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 -----------------------------------------------------------------------------
17 module GHC.TopHandler (
18 runMainIO, runIO, runNonIO, reportStackOverflow, reportError
24 import Control.Exception
28 import GHC.Prim (unsafeCoerce#)
30 -- | 'runMainIO' is wrapped around 'Main.main' (or whatever main is
31 -- called in the program). It catches otherwise uncaught exceptions,
32 -- and also flushes stdout\/stderr before exiting.
33 runMainIO :: IO a -> IO a
34 runMainIO main = (do a <- main; cleanUp; return a) `catchException` topHandler
36 -- | 'runIO' is wrapped around every @foreign export@ and @foreign
37 -- import \"wrapper\"@ to mop up any uncaught exceptions. Thus, the
38 -- result of running 'System.Exit.exitWith' in a foreign-exported
39 -- function is the same as in the main thread: it terminates the
43 runIO main = catchException main topHandler
45 -- | The same as 'runIO', but for non-IO computations. Used for
46 -- wrapping @foreign export@ and @foreign import \"wrapper\"@ when these
47 -- are used to export Haskell functions with non-IO types.
50 runNonIO a = catchException (a `seq` return a) topHandler
52 topHandler :: Exception -> IO a
53 topHandler err = catchException (real_handler err) topHandler
55 -- Make sure we handle errors while reporting the error!
56 -- (e.g. evaluating the string passed to 'error' might generate
57 -- another error, etc.)
59 real_handler :: Exception -> IO a
63 AsyncException StackOverflow -> do
67 -- only the main thread gets ExitException exceptions
68 ExitException ExitSuccess -> safeExit 0
69 ExitException (ExitFailure n) -> safeExit n
76 reportStackOverflow :: IO a
77 reportStackOverflow = do callStackOverflowHook; return undefined
79 reportError :: Exception -> IO a
81 handler <- getUncaughtExceptionHandler
85 -- SUP: Are the hooks allowed to re-enter Haskell land? If so, remove
87 foreign import ccall unsafe "stackOverflow"
88 callStackOverflowHook :: IO ()
90 -- try to flush stdout/stderr, but don't worry if we fail
91 -- (these handles might have errors, and we don't want to go into
95 hFlush stdout `catchException` \_ -> return ()
96 hFlush stderr `catchException` \_ -> return ()
98 cleanUpAndExit :: Int -> IO a
99 cleanUpAndExit r = do cleanUp; safeExit r
101 -- we have to use unsafeCoerce# to get the 'IO a' result type, since the
102 -- compiler doesn't let us declare that as the result type of a foreign export.
103 safeExit :: Int -> IO a
104 safeExit r = unsafeCoerce# (shutdownHaskellAndExit r)
106 -- NOTE: shutdownHaskellAndExit must be called "safe", because it *can*
107 -- re-enter Haskell land through finalizers.
108 foreign import ccall "shutdownHaskellAndExit"
109 shutdownHaskellAndExit :: Int -> IO ()