3 -----------------------------------------------------------------------------
5 -- Module : System.Exit
6 -- Copyright : (c) The University of Glasgow 2001
7 -- License : BSD-style (see the file libraries/base/LICENSE)
9 -- Maintainer : libraries@haskell.org
10 -- Stability : provisional
11 -- Portability : portable
13 -- Exiting the program.
15 -----------------------------------------------------------------------------
19 ExitCode(ExitSuccess,ExitFailure)
20 , exitWith -- :: ExitCode -> IO a
21 , exitFailure -- :: IO a
22 , exitSuccess -- :: IO a
27 #ifdef __GLASGOW_HASKELL__
29 import GHC.IO.Exception
33 import Hugs.Prelude (ExitCode(..))
34 import Control.Exception.Base
44 -- ---------------------------------------------------------------------------
47 -- | Computation 'exitWith' @code@ throws 'ExitCode' @code@.
48 -- Normally this terminates the program, returning @code@ to the
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
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'.
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'.
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.
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)
81 #endif /* ! __NHC__ */
83 -- | The computation 'exitFailure' is equivalent to
84 -- 'exitWith' @(@'ExitFailure' /exitfail/@)@,
85 -- where /exitfail/ is implementation-dependent.
87 exitFailure = exitWith (ExitFailure 1)
89 -- | The computation 'exitSuccess' is equivalent to
90 -- 'exitWith' 'ExitSuccess', It terminates the program
93 exitSuccess = exitWith ExitSuccess