[project @ 2003-08-30 22:55:42 by ross]
[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   ) where
21
22 import Prelude
23
24 #ifdef __GLASGOW_HASKELL__
25 import GHC.IOBase
26 #endif
27
28 #ifdef __HUGS__
29 import Hugs.Prelude
30 import Hugs.Exception
31 #endif
32
33 #ifdef __NHC__
34 import System
35   ( ExitCode(..)
36   , exitWith
37   )
38 #endif
39
40 -- ---------------------------------------------------------------------------
41 -- exitWith
42
43 -- | Computation 'exitWith' @code@ throws 'ExitException' @code@.
44 -- Normally this terminates the program, returning @code@ to the
45 -- program's caller.  Before the program terminates, any open or
46 -- semi-closed handles are first closed.
47 --
48 -- A program that fails in any other way is treated as if it had
49 -- called 'exitFailure'.
50 -- A program that terminates successfully without calling 'exitWith'
51 -- explicitly is treated as it it had called 'exitWith' 'ExitSuccess'.
52 --
53 -- As an 'ExitException' is not an 'IOError', 'exitWith' bypasses
54 -- the error handling in the 'IO' monad and cannot be intercepted by
55 -- 'catch' from the "Prelude".  However it is an 'Exception', and can
56 -- be caught using the functions of "Control.Exception".  This means
57 -- that cleanup computations added with 'Control.Exception.bracket'
58 -- (from "Control.Exception") are also executed properly on 'exitWith'.
59
60 #ifndef __NHC__
61 exitWith :: ExitCode -> IO a
62 exitWith ExitSuccess = throwIO (ExitException ExitSuccess)
63 exitWith code@(ExitFailure n)
64   | n /= 0 = throwIO (ExitException code)
65 #ifdef __GLASGOW_HASKELL__
66   | otherwise = ioError (IOError Nothing InvalidArgument "exitWith" "ExitFailure 0" Nothing)
67 #endif
68 #endif  /* ! __NHC__ */
69
70 -- | The computation 'exitFailure' is equivalent to
71 -- 'exitWith' @(@'ExitFailure' /exitfail/@)@,
72 -- where /exitfail/ is implementation-dependent.
73 exitFailure :: IO a
74 exitFailure = exitWith (ExitFailure 1)