Update the exitWith docs
[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.IO
27 import GHC.IO.Exception
28 #endif
29
30 #ifdef __HUGS__
31 import Hugs.Prelude (ExitCode(..))
32 import Control.Exception.Base
33 #endif
34
35 #ifdef __NHC__
36 import System
37   ( ExitCode(..)
38   , exitWith
39   )
40 #endif
41
42 -- ---------------------------------------------------------------------------
43 -- exitWith
44
45 -- | Computation 'exitWith' @code@ throws 'ExitCode' @code@.
46 -- Normally this terminates the program, returning @code@ to the
47 -- program's caller.  Before the program terminates, any open or
48 -- semi-closed handles are first closed.
49 --
50 -- A program that fails in any other way is treated as if it had
51 -- called 'exitFailure'.
52 -- A program that terminates successfully without calling 'exitWith'
53 -- explicitly is treated as it it had called 'exitWith' 'ExitSuccess'.
54 --
55 -- As an 'ExitCode' is not an 'IOError', 'exitWith' bypasses
56 -- the error handling in the 'IO' monad and cannot be intercepted by
57 -- 'catch' from the "Prelude".  However it is a 'SomeException', and can
58 -- be caught using the functions of "Control.Exception".  This means
59 -- that cleanup computations added with 'Control.Exception.bracket'
60 -- (from "Control.Exception") are also executed properly on 'exitWith'.
61 --
62 -- Note: in GHC, 'exitWith' should be called from the main program
63 -- thread in order to exit the process.  When called from another
64 -- thread, 'exitWith' will throw an 'ExitException' as normal, but the
65 -- exception will not cause the process itself to exit.
66 --
67 #ifndef __NHC__
68 exitWith :: ExitCode -> IO a
69 exitWith ExitSuccess = throwIO ExitSuccess
70 exitWith code@(ExitFailure n)
71   | n /= 0 = throwIO code
72 #ifdef __GLASGOW_HASKELL__
73   | otherwise = ioError (IOError Nothing InvalidArgument "exitWith" "ExitFailure 0" Nothing Nothing)
74 #endif
75 #endif  /* ! __NHC__ */
76
77 -- | The computation 'exitFailure' is equivalent to
78 -- 'exitWith' @(@'ExitFailure' /exitfail/@)@,
79 -- where /exitfail/ is implementation-dependent.
80 exitFailure :: IO a
81 exitFailure = exitWith (ExitFailure 1)
82
83 -- | The computation 'exitSuccess' is equivalent to
84 -- 'exitWith' 'ExitSuccess', It terminates the program
85 -- sucessfully.
86 exitSuccess :: IO a
87 exitSuccess = exitWith ExitSuccess