Exception.hs 974 Bytes
Newer Older
1 2 3

module Exception
    (
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
    module Control.Exception,
    module Exception
    )
    where

import Prelude hiding (catch)
import Control.Exception

#if __GLASGOW_HASKELL__ < 609
type SomeException = Exception

onException :: IO a -> IO () -> IO a
onException io what = io `catch` \e -> do what
                                          throw e
#endif

catchIO :: IO a -> (IOException -> IO a) -> IO a
21
#if __GLASGOW_HASKELL__ >= 609
22
catchIO = catch
23
#else
24 25 26
catchIO io handler = io `catch` handler'
    where handler' (IOException ioe) = handler ioe
          handler' e                 = throw e
27 28
#endif

29 30
handleIO :: (IOException -> IO a) -> IO a -> IO a
handleIO = flip catchIO
31

32
tryIO :: IO a -> IO (Either IOException a)
33
#if __GLASGOW_HASKELL__ >= 609
34
tryIO = try
35
#else
36 37 38 39 40
tryIO io = do ei <- try io
              case ei of
                  Right v -> return (Right v)
                  Left (IOException ioe) -> return (Left ioe)
                  Left e -> throwIO e
41 42
#endif