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