exception-transformers-0.4.0.10: Type classes and monads for unchecked extensible exceptions.
Copyright(c) Harvard University 2008-2011
(c) Geoffrey Mainland 2011-2016
LicenseBSD-style
Maintainermainland@cs.drexel.edu
Safe HaskellNone
LanguageHaskell98

Control.Monad.Exception

Description

 
Synopsis

Documentation

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

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

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

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 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

Minimal complete definition

Nothing

Methods

toException :: e -> SomeException #

fromException :: SomeException -> Maybe e #

displayException :: e -> String #

Render this exception value in a human-friendly manner.

Default implementation: show.

Since: base-4.8.0.0

Instances

Instances details
Exception PatternMatchFail

Since: base-4.0

Instance details

Defined in Control.Exception.Base

Exception RecSelError

Since: base-4.0

Instance details

Defined in Control.Exception.Base

Exception RecConError

Since: base-4.0

Instance details

Defined in Control.Exception.Base

Exception RecUpdError

Since: base-4.0

Instance details

Defined in Control.Exception.Base

Exception NoMethodError

Since: base-4.0

Instance details

Defined in Control.Exception.Base

Exception TypeError

Since: base-4.9.0.0

Instance details

Defined in Control.Exception.Base

Exception NonTermination

Since: base-4.0

Instance details

Defined in Control.Exception.Base

Exception NestedAtomically

Since: base-4.0

Instance details

Defined in Control.Exception.Base

Exception BlockedIndefinitelyOnMVar

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception BlockedIndefinitelyOnSTM

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception Deadlock

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception AllocationLimitExceeded

Since: base-4.8.0.0

Instance details

Defined in GHC.IO.Exception

Exception CompactionFailed

Since: base-4.10.0.0

Instance details

Defined in GHC.IO.Exception

Exception AssertionFailed

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception SomeAsyncException

Since: base-4.7.0.0

Instance details

Defined in GHC.IO.Exception

Exception AsyncException

Since: base-4.7.0.0

Instance details

Defined in GHC.IO.Exception

Exception ArrayException

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception FixIOException

Since: base-4.11.0.0

Instance details

Defined in GHC.IO.Exception

Exception ExitCode

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception IOException

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception ErrorCall

Since: base-4.0.0.0

Instance details

Defined in GHC.Exception

Exception ArithException

Since: base-4.0.0.0

Instance details

Defined in GHC.Exception.Type

Exception SomeException

Since: base-3.0

Instance details

Defined in GHC.Exception.Type

data SomeException #

The SomeException type is the root of the exception type hierarchy. When an exception of type e is thrown, behind the scenes it is encapsulated in a SomeException.

Instances

Instances details
Show SomeException

Since: base-3.0

Instance details

Defined in GHC.Exception.Type

Exception SomeException

Since: base-3.0

Instance details

Defined in GHC.Exception.Type

class Monad m => MonadException m where Source #

Minimal complete definition

throw, catch

Methods

throw :: Exception e => e -> m a Source #

Throw an exception.

catch Source #

Arguments

:: Exception e 
=> m a

The computation to run

-> (e -> m a)

Handler to invoke if an exception is raised

-> m a 

Catch an exception.

finally Source #

Arguments

:: m a

The computation to run

-> m b

Computation to run afterward (even if an exception was raised)

-> m a 

Run a computation and always perform a second, final computation even if an exception is raised. If a short-circuiting monad transformer such as ErrorT or MaybeT is used to transform a MonadException monad, then the implementation of finally for the transformed monad must guarantee that the final action is also always performed when any short-circuiting occurs.

Instances

Instances details
MonadException IO Source # 
Instance details

Defined in Control.Monad.Exception

Methods

throw :: Exception e => e -> IO a Source #

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

finally :: IO a -> IO b -> IO a Source #

MonadException STM Source # 
Instance details

Defined in Control.Monad.Exception

Methods

throw :: Exception e => e -> STM a Source #

catch :: Exception e => STM a -> (e -> STM a) -> STM a Source #

finally :: STM a -> STM b -> STM a Source #

MonadException m => MonadException (MaybeT m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

throw :: Exception e => e -> MaybeT m a Source #

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

finally :: MaybeT m a -> MaybeT m b -> MaybeT m a Source #

MonadException m => MonadException (ListT m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

throw :: Exception e => e -> ListT m a Source #

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

finally :: ListT m a -> ListT m b -> ListT m a Source #

Monad m => MonadException (ExceptionT m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

throw :: Exception e => e -> ExceptionT m a Source #

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

finally :: ExceptionT m a -> ExceptionT m b -> ExceptionT m a Source #

(Monoid w, MonadException m) => MonadException (WriterT w m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

throw :: Exception e => e -> WriterT w m a Source #

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

finally :: WriterT w m a -> WriterT w m b -> WriterT w m a Source #

(Monoid w, MonadException m) => MonadException (WriterT w m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

throw :: Exception e => e -> WriterT w m a Source #

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

finally :: WriterT w m a -> WriterT w m b -> WriterT w m a Source #

MonadException m => MonadException (StateT s m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

throw :: Exception e => e -> StateT s m a Source #

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

finally :: StateT s m a -> StateT s m b -> StateT s m a Source #

MonadException m => MonadException (StateT s m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

throw :: Exception e => e -> StateT s m a Source #

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

finally :: StateT s m a -> StateT s m b -> StateT s m a Source #

MonadException m => MonadException (ReaderT r m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

throw :: Exception e => e -> ReaderT r m a Source #

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

finally :: ReaderT r m a -> ReaderT r m b -> ReaderT r m a Source #

MonadException m => MonadException (IdentityT m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

throw :: Exception e => e -> IdentityT m a Source #

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

finally :: IdentityT m a -> IdentityT m b -> IdentityT m a Source #

MonadException m => MonadException (ExceptT e' m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

throw :: Exception e => e -> ExceptT e' m a Source #

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

finally :: ExceptT e' m a -> ExceptT e' m b -> ExceptT e' m a Source #

(MonadException m, Error e) => MonadException (ErrorT e m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

throw :: Exception e0 => e0 -> ErrorT e m a Source #

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

finally :: ErrorT e m a -> ErrorT e m b -> ErrorT e m a Source #

(Monoid w, MonadException m) => MonadException (RWST r w s m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

throw :: Exception e => e -> RWST r w s m a Source #

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

finally :: RWST r w s m a -> RWST r w s m b -> RWST r w s m a Source #

(Monoid w, MonadException m) => MonadException (RWST r w s m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

throw :: Exception e => e -> RWST r w s m a Source #

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

finally :: RWST r w s m a -> RWST r w s m b -> RWST r w s m a Source #

onException Source #

Arguments

:: MonadException m 
=> m a

The computation to run

-> m b

Computation to run if an exception is raised

-> m a 

If an exception is raised by the computation, then perform a final action and re-raise the exception.

class (MonadIO m, MonadException m) => MonadAsyncException m where Source #

Methods

mask :: ((forall a. m a -> m a) -> m b) -> m b Source #

Executes a computation with asynchronous exceptions masked. The argument passed to mask is a function that takes as its argument another function, which can be used to restore the prevailing masking state within the context of the masked computation.

Instances

Instances details
MonadAsyncException IO Source # 
Instance details

Defined in Control.Monad.Exception

Methods

mask :: ((forall a. IO a -> IO a) -> IO b) -> IO b Source #

MonadAsyncException m => MonadAsyncException (MaybeT m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

mask :: ((forall a. MaybeT m a -> MaybeT m a) -> MaybeT m b) -> MaybeT m b Source #

MonadAsyncException m => MonadAsyncException (ListT m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

mask :: ((forall a. ListT m a -> ListT m a) -> ListT m b) -> ListT m b Source #

MonadAsyncException m => MonadAsyncException (ExceptionT m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

mask :: ((forall a. ExceptionT m a -> ExceptionT m a) -> ExceptionT m b) -> ExceptionT m b Source #

(Monoid w, MonadAsyncException m) => MonadAsyncException (WriterT w m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

mask :: ((forall a. WriterT w m a -> WriterT w m a) -> WriterT w m b) -> WriterT w m b Source #

(Monoid w, MonadAsyncException m) => MonadAsyncException (WriterT w m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

mask :: ((forall a. WriterT w m a -> WriterT w m a) -> WriterT w m b) -> WriterT w m b Source #

MonadAsyncException m => MonadAsyncException (StateT s m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

mask :: ((forall a. StateT s m a -> StateT s m a) -> StateT s m b) -> StateT s m b Source #

MonadAsyncException m => MonadAsyncException (StateT s m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

mask :: ((forall a. StateT s m a -> StateT s m a) -> StateT s m b) -> StateT s m b Source #

MonadAsyncException m => MonadAsyncException (ReaderT r m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

mask :: ((forall a. ReaderT r m a -> ReaderT r m a) -> ReaderT r m b) -> ReaderT r m b Source #

MonadAsyncException m => MonadAsyncException (IdentityT m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

mask :: ((forall a. IdentityT m a -> IdentityT m a) -> IdentityT m b) -> IdentityT m b Source #

MonadAsyncException m => MonadAsyncException (ExceptT e' m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

mask :: ((forall a. ExceptT e' m a -> ExceptT e' m a) -> ExceptT e' m b) -> ExceptT e' m b Source #

(MonadAsyncException m, Error e) => MonadAsyncException (ErrorT e m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

mask :: ((forall a. ErrorT e m a -> ErrorT e m a) -> ErrorT e m b) -> ErrorT e m b Source #

(Monoid w, MonadAsyncException m) => MonadAsyncException (RWST r w s m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

mask :: ((forall a. RWST r w s m a -> RWST r w s m a) -> RWST r w s m b) -> RWST r w s m b Source #

(Monoid w, MonadAsyncException m) => MonadAsyncException (RWST r w s m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

mask :: ((forall a. RWST r w s m a -> RWST r w s m a) -> RWST r w s m b) -> RWST r w s m b Source #

bracket Source #

Arguments

:: MonadAsyncException m 
=> m a

computation to run first ("acquire resource")

-> (a -> m b)

computation to run last ("release resource")

-> (a -> m c)

computation to run in-between

-> m c 

When you want to acquire a resource, do some work with it, and then release the resource, it is a good idea to use bracket, because bracket will install the necessary exception handler to release the resource in the event that an exception is raised during the computation. If an exception is raised, then bracket will re-raise the exception (after performing the release).

bracket_ :: MonadAsyncException m => m a -> m b -> m c -> m c Source #

A variant of bracket where the return value from the first computation is not required.

newtype ExceptionT m a Source #

Constructors

ExceptionT 

Instances

Instances details
MonadTrans ExceptionT Source # 
Instance details

Defined in Control.Monad.Exception

Methods

lift :: Monad m => m a -> ExceptionT m a #

Monad m => Monad (ExceptionT m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

(>>=) :: ExceptionT m a -> (a -> ExceptionT m b) -> ExceptionT m b #

(>>) :: ExceptionT m a -> ExceptionT m b -> ExceptionT m b #

return :: a -> ExceptionT m a #

Functor m => Functor (ExceptionT m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

fmap :: (a -> b) -> ExceptionT m a -> ExceptionT m b #

(<$) :: a -> ExceptionT m b -> ExceptionT m a #

MonadFix m => MonadFix (ExceptionT m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

mfix :: (a -> ExceptionT m a) -> ExceptionT m a #

Monad m => MonadFail (ExceptionT m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

fail :: String -> ExceptionT m a #

(Functor m, Monad m) => Applicative (ExceptionT m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

pure :: a -> ExceptionT m a #

(<*>) :: ExceptionT m (a -> b) -> ExceptionT m a -> ExceptionT m b #

liftA2 :: (a -> b -> c) -> ExceptionT m a -> ExceptionT m b -> ExceptionT m c #

(*>) :: ExceptionT m a -> ExceptionT m b -> ExceptionT m b #

(<*) :: ExceptionT m a -> ExceptionT m b -> ExceptionT m a #

MonadIO m => MonadIO (ExceptionT m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

liftIO :: IO a -> ExceptionT m a #

(Functor m, Monad m) => Alternative (ExceptionT m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

empty :: ExceptionT m a #

(<|>) :: ExceptionT m a -> ExceptionT m a -> ExceptionT m a #

some :: ExceptionT m a -> ExceptionT m [a] #

many :: ExceptionT m a -> ExceptionT m [a] #

Monad m => MonadPlus (ExceptionT m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

mzero :: ExceptionT m a #

mplus :: ExceptionT m a -> ExceptionT m a -> ExceptionT m a #

MonadAsyncException m => MonadAsyncException (ExceptionT m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

mask :: ((forall a. ExceptionT m a -> ExceptionT m a) -> ExceptionT m b) -> ExceptionT m b Source #

Monad m => MonadException (ExceptionT m) Source # 
Instance details

Defined in Control.Monad.Exception

Methods

throw :: Exception e => e -> ExceptionT m a Source #

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

finally :: ExceptionT m a -> ExceptionT m b -> ExceptionT m a Source #

liftException :: MonadException m => Either SomeException a -> m a Source #

Lift the result of running a computation in a monad transformed by ExceptionT into another monad that supports exceptions.