exception-transformers-0.4.0.6: 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

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

Instances

Exception PatternMatchFail

Since: 4.0

Exception RecSelError

Since: 4.0

Exception RecConError

Since: 4.0

Exception RecUpdError

Since: 4.0

Exception NoMethodError

Since: 4.0

Exception TypeError

Since: 4.9.0.0

Exception NonTermination

Since: 4.0

Exception NestedAtomically

Since: 4.0

Exception BlockedIndefinitelyOnMVar

Since: 4.1.0.0

Exception BlockedIndefinitelyOnSTM

Since: 4.1.0.0

Exception Deadlock

Since: 4.1.0.0

Exception AllocationLimitExceeded

Since: 4.8.0.0

Exception CompactionFailed

Since: 4.10.0.0

Exception AssertionFailed

Since: 4.1.0.0

Exception SomeAsyncException

Since: 4.7.0.0

Exception AsyncException

Since: 4.7.0.0

Exception ArrayException

Since: 4.1.0.0

Exception ExitCode

Since: 4.1.0.0

Exception IOException

Since: 4.1.0.0

Exception ErrorCall

Since: 4.0.0.0

Exception ArithException

Since: 4.0.0.0

Exception SomeException

Since: 3.0

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.

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

MonadException IO Source # 

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 # 

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 # 

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 # 

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 # 

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 # 

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 # 

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 # 

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 # 

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 (IdentityT * m) Source # 

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 # 

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 # 

Methods

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

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

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

MonadException m => MonadException (ReaderT * r m) Source # 

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 #

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

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 # 

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 #

Minimal complete definition

mask

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

MonadAsyncException IO Source # 

Methods

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

MonadAsyncException m => MonadAsyncException (MaybeT m) Source # 

Methods

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

MonadAsyncException m => MonadAsyncException (ListT m) Source # 

Methods

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

MonadAsyncException m => MonadAsyncException (ExceptionT m) Source # 

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 # 

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 # 

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 # 

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 # 

Methods

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

MonadAsyncException m => MonadAsyncException (IdentityT * m) Source # 

Methods

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

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

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 # 

Methods

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

MonadAsyncException m => MonadAsyncException (ReaderT * r m) Source # 

Methods

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

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

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 # 

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

MonadTrans ExceptionT Source # 

Methods

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

Monad m => Monad (ExceptionT m) Source # 

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 #

fail :: String -> ExceptionT m a #

Functor m => Functor (ExceptionT m) Source # 

Methods

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

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

MonadFix m => MonadFix (ExceptionT m) Source # 

Methods

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

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

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 # 

Methods

liftIO :: IO a -> ExceptionT m a #

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

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 # 

Methods

mzero :: ExceptionT m a #

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

MonadAsyncException m => MonadAsyncException (ExceptionT m) Source # 

Methods

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

Monad m => MonadException (ExceptionT m) Source # 

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.