7cca0174a0b2c4490bd0fc322101f75b02758d5a
[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.
48 --
49 -- On program termination, the standard 'Handle's 'stdout' and
50 -- 'stderr' are flushed automatically; any other buffered 'Handle's
51 -- need to be flushed manually, otherwise the buffered data will be
52 -- discarded.
53 --
54 -- A program that fails in any other way is treated as if it had
55 -- called 'exitFailure'.
56 -- A program that terminates successfully without calling 'exitWith'
57 -- explicitly is treated as it it had called 'exitWith' 'ExitSuccess'.
58 --
59 -- As an 'ExitCode' is not an 'IOError', 'exitWith' bypasses
60 -- the error handling in the 'IO' monad and cannot be intercepted by
61 -- 'catch' from the "Prelude".  However it is a 'SomeException', and can
62 -- be caught using the functions of "Control.Exception".  This means
63 -- that cleanup computations added with 'Control.Exception.bracket'
64 -- (from "Control.Exception") are also executed properly on 'exitWith'.
65 --
66 -- Note: in GHC, 'exitWith' should be called from the main program
67 -- thread in order to exit the process.  When called from another
68 -- thread, 'exitWith' will throw an 'ExitException' as normal, but the
69 -- exception will not cause the process itself to exit.
70 --
71 #ifndef __NHC__
72 exitWith :: ExitCode -> IO a
73 exitWith ExitSuccess = throwIO ExitSuccess
74 exitWith code@(ExitFailure n)
75   | n /= 0 = throwIO code
76 #ifdef __GLASGOW_HASKELL__
77   | otherwise = ioError (IOError Nothing InvalidArgument "exitWith" "ExitFailure 0" Nothing Nothing)
78 #endif
79 #endif  /* ! __NHC__ */
80
81 -- | The computation 'exitFailure' is equivalent to
82 -- 'exitWith' @(@'ExitFailure' /exitfail/@)@,
83 -- where /exitfail/ is implementation-dependent.
84 exitFailure :: IO a
85 exitFailure = exitWith (ExitFailure 1)
86
87 -- | The computation 'exitSuccess' is equivalent to
88 -- 'exitWith' 'ExitSuccess', It terminates the program
89 -- successfully.
90 exitSuccess :: IO a
91 exitSuccess = exitWith ExitSuccess