MonadCatchIO-mtl-0.3.1.0: Monad-transformer version of the Control.Exception module

Safe HaskellNone
LanguageHaskell98

Control.Monad.CatchIO

Synopsis

Documentation

class MonadIO m => MonadCatchIO m where #

Minimal complete definition

catch, block, unblock

Methods

catch :: Exception e => m a -> (e -> m a) -> m a #

block :: m a -> m a #

unblock :: m a -> m a #

Instances

MonadCatchIO IO 

Methods

catch :: Exception e => IO a -> (e -> IO a) -> IO a #

block :: IO a -> IO a #

unblock :: IO a -> IO a #

MonadCatchIO m => MonadCatchIO (MaybeT m) 

Methods

catch :: Exception e => MaybeT m a -> (e -> MaybeT m a) -> MaybeT m a #

block :: MaybeT m a -> MaybeT m a #

unblock :: MaybeT m a -> MaybeT m a #

MonadCatchIO m => MonadCatchIO (ListT m) 

Methods

catch :: Exception e => ListT m a -> (e -> ListT m a) -> ListT m a #

block :: ListT m a -> ListT m a #

unblock :: ListT m a -> ListT m a #

(Monoid w, MonadCatchIO m) => MonadCatchIO (WriterT w m) 

Methods

catch :: Exception e => WriterT w m a -> (e -> WriterT w m a) -> WriterT w m a #

block :: WriterT w m a -> WriterT w m a #

unblock :: WriterT w m a -> WriterT w m a #

(Monoid w, MonadCatchIO m) => MonadCatchIO (WriterT w m) 

Methods

catch :: Exception e => WriterT w m a -> (e -> WriterT w m a) -> WriterT w m a #

block :: WriterT w m a -> WriterT w m a #

unblock :: WriterT w m a -> WriterT w m a #

MonadCatchIO m => MonadCatchIO (StateT s m) 

Methods

catch :: Exception e => StateT s m a -> (e -> StateT s m a) -> StateT s m a #

block :: StateT s m a -> StateT s m a #

unblock :: StateT s m a -> StateT s m a #

MonadCatchIO m => MonadCatchIO (StateT s m) 

Methods

catch :: Exception e => StateT s m a -> (e -> StateT s m a) -> StateT s m a #

block :: StateT s m a -> StateT s m a #

unblock :: StateT s m a -> StateT s m a #

MonadCatchIO m => MonadCatchIO (IdentityT * m) 

Methods

catch :: Exception e => IdentityT * m a -> (e -> IdentityT * m a) -> IdentityT * m a #

block :: IdentityT * m a -> IdentityT * m a #

unblock :: IdentityT * m a -> IdentityT * m a #

(MonadCatchIO m, Error e) => MonadCatchIO (ErrorT e m) 

Methods

catch :: Exception e => ErrorT e m a -> (e -> ErrorT e m a) -> ErrorT e m a #

block :: ErrorT e m a -> ErrorT e m a #

unblock :: ErrorT e m a -> ErrorT e m a #

MonadCatchIO m => MonadCatchIO (ReaderT * r m) 

Methods

catch :: Exception e => ReaderT * r m a -> (e -> ReaderT * r m a) -> ReaderT * r m a #

block :: ReaderT * r m a -> ReaderT * r m a #

unblock :: ReaderT * r m a -> ReaderT * r m a #

MonadCatchIO m => MonadCatchIO (ContT * r m) 

Methods

catch :: Exception e => ContT * r m a -> (e -> ContT * r m a) -> ContT * r m a #

block :: ContT * r m a -> ContT * r m a #

unblock :: ContT * r m a -> ContT * r m a #

(Monoid w, MonadCatchIO m) => MonadCatchIO (RWST r w s m) 

Methods

catch :: Exception e => RWST r w s m a -> (e -> RWST r w s m a) -> RWST r w s m a #

block :: RWST r w s m a -> RWST r w s m a #

unblock :: RWST r w s m a -> RWST r w s m a #

(Monoid w, MonadCatchIO m) => MonadCatchIO (RWST r w s m) 

Methods

catch :: Exception e => RWST r w s m a -> (e -> RWST r w s m a) -> RWST r w s m a #

block :: RWST r w s m a -> RWST r w s m a #

unblock :: RWST r w s m a -> RWST r w s m a #

class (Typeable * e, Show e) => Exception e where #

Any type that you wish to throw or catch as an exception must be an instance of the Exception class. The simplest case is a new exception type directly below the root:

data MyException = ThisException | ThatException
    deriving (Show, Typeable)

instance Exception MyException

The default method definitions in the Exception class do what we need in this case. You can now throw and catch ThisException and ThatException as exceptions:

*Main> throw ThisException `catch` \e -> putStrLn ("Caught " ++ show (e :: MyException))
Caught ThisException

In more complicated examples, you may wish to define a whole hierarchy of exceptions:

---------------------------------------------------------------------
-- Make the root exception type for all the exceptions in a compiler

data SomeCompilerException = forall e . Exception e => SomeCompilerException e
    deriving Typeable

instance Show SomeCompilerException where
    show (SomeCompilerException e) = show e

instance Exception SomeCompilerException

compilerExceptionToException :: Exception e => e -> SomeException
compilerExceptionToException = toException . SomeCompilerException

compilerExceptionFromException :: Exception e => SomeException -> Maybe e
compilerExceptionFromException x = do
    SomeCompilerException a <- fromException x
    cast a

---------------------------------------------------------------------
-- Make a subhierarchy for exceptions in the frontend of the compiler

data SomeFrontendException = forall e . Exception e => SomeFrontendException e
    deriving Typeable

instance Show SomeFrontendException where
    show (SomeFrontendException e) = show e

instance Exception SomeFrontendException where
    toException = compilerExceptionToException
    fromException = compilerExceptionFromException

frontendExceptionToException :: Exception e => e -> SomeException
frontendExceptionToException = toException . SomeFrontendException

frontendExceptionFromException :: Exception e => SomeException -> Maybe e
frontendExceptionFromException x = do
    SomeFrontendException a <- fromException x
    cast a

---------------------------------------------------------------------
-- Make an exception type for a particular frontend compiler exception

data MismatchedParentheses = MismatchedParentheses
    deriving (Typeable, Show)

instance Exception MismatchedParentheses where
    toException   = frontendExceptionToException
    fromException = frontendExceptionFromException

We can now catch a MismatchedParentheses exception as MismatchedParentheses, SomeFrontendException or SomeCompilerException, but not other types, e.g. IOException:

*Main> throw MismatchedParentheses catch e -> putStrLn ("Caught " ++ show (e :: MismatchedParentheses))
Caught MismatchedParentheses
*Main> throw MismatchedParentheses catch e -> putStrLn ("Caught " ++ show (e :: SomeFrontendException))
Caught MismatchedParentheses
*Main> throw MismatchedParentheses catch e -> putStrLn ("Caught " ++ show (e :: SomeCompilerException))
Caught MismatchedParentheses
*Main> throw MismatchedParentheses catch e -> putStrLn ("Caught " ++ show (e :: IOException))
*** Exception: MismatchedParentheses

Methods

toException :: e -> SomeException #

fromException :: SomeException -> Maybe e #

displayException :: e -> String #

Render this exception value in a human-friendly manner.

Default implementation: show.

Since: 4.8.0.0

throw :: (MonadIO m, Exception e) => e -> m a #

try :: (MonadCatchIO m, Functor m, Exception e) => m a -> m (Either e a) #

tryJust :: (MonadCatchIO m, Functor m, Exception e) => (e -> Maybe b) -> m a -> m (Either b a) #

onException :: MonadCatchIO m => m a -> m b -> m a #

bracket :: MonadCatchIO m => m a -> (a -> m b) -> (a -> m c) -> m c #

bracket_ :: MonadCatchIO m => m a -> m b -> m c -> m c #

finally :: MonadCatchIO m => m a -> m b -> m a #

bracketOnError :: MonadCatchIO m => m a -> (a -> m b) -> (a -> m c) -> m c #

data Handler m a :: (* -> *) -> * -> * where #

Constructors

Handler :: Handler m a 

catches :: MonadCatchIO m => m a -> [Handler m a] -> m a #