Fix #3257: document that exitWith in a forkIO'd thread does not exit the process
[ghc-base.git] / System / Exit.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module      :  System.Exit
4 -- Copyright   :  (c) The University of Glasgow 2001
5 -- License     :  BSD-style (see the file libraries/base/LICENSE)
6 -- 
7 -- Maintainer  :  libraries@haskell.org
8 -- Stability   :  provisional
9 -- Portability :  portable
10 --
11 -- Exiting the program.
12 --
13 -----------------------------------------------------------------------------
14
15 module System.Exit
16     (
17       ExitCode(ExitSuccess,ExitFailure)
18     , exitWith      -- :: ExitCode -> IO a
19     , exitFailure   -- :: IO a
20     , exitSuccess   -- :: IO a
21   ) where
22
23 import Prelude
24
25 #ifdef __GLASGOW_HASKELL__
26 import GHC.IOBase
27 #endif
28
29 #ifdef __HUGS__
30 import Hugs.Prelude (ExitCode(..))
31 import Control.Exception.Base
32 #endif
33
34 #ifdef __NHC__
35 import System
36   ( ExitCode(..)
37   , exitWith
38   )
39 #endif
40
41 -- ---------------------------------------------------------------------------
42 -- exitWith
43
44 -- | Computation 'exitWith' @code@ throws 'ExitException' @code@.
45 -- Normally this terminates the program, returning @code@ to the
46 -- program's caller.  Before the program terminates, any open or
47 -- semi-closed handles are first closed.
48 --
49 -- A program that fails in any other way is treated as if it had
50 -- called 'exitFailure'.
51 -- A program that terminates successfully without calling 'exitWith'
52 -- explicitly is treated as it it had called 'exitWith' 'ExitSuccess'.
53 --
54 -- As an 'ExitException' is not an 'IOError', 'exitWith' bypasses
55 -- the error handling in the 'IO' monad and cannot be intercepted by
56 -- 'catch' from the "Prelude".  However it is an 'Exception', and can
57 -- be caught using the functions of "Control.Exception".  This means
58 -- that cleanup computations added with 'Control.Exception.bracket'
59 -- (from "Control.Exception") are also executed properly on 'exitWith'.
60 --
61 -- Note: in GHC, 'exitWith' should be called from the main program
62 -- thread in order to exit the process.  When called from another
63 -- thread, 'exitWith' will throw an 'ExitException' as normal, but the
64 -- exception will not cause the process itself to exit.
65 --
66 #ifndef __NHC__
67 exitWith :: ExitCode -> IO a
68 exitWith ExitSuccess = throwIO ExitSuccess
69 exitWith code@(ExitFailure n)
70   | n /= 0 = throwIO code
71 #ifdef __GLASGOW_HASKELL__
72   | otherwise = ioError (IOError Nothing InvalidArgument "exitWith" "ExitFailure 0" Nothing Nothing)
73 #endif
74 #endif  /* ! __NHC__ */
75
76 -- | The computation 'exitFailure' is equivalent to
77 -- 'exitWith' @(@'ExitFailure' /exitfail/@)@,
78 -- where /exitfail/ is implementation-dependent.
79 exitFailure :: IO a
80 exitFailure = exitWith (ExitFailure 1)
81
82 -- | The computation 'exitSuccess' is equivalent to
83 -- 'exitWith' 'ExitSuccess', It terminates the program
84 -- sucessfully.
85 exitSuccess :: IO a
86 exitSuccess = exitWith ExitSuccess