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