Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
The Eff
monad comes with instances of MonadThrow
, MonadCatch
and
MonadMask
from the
exceptions
library, so
this module simply re-exports the interface of the
safe-exceptions
library.
Why safe-exceptions
and not exceptions
? Because the former makes it much
easier to correctly deal with asynchronous exceptions (for more information
see its README) and
provides more convenience functions.
Synopsis
- class Monad m => MonadThrow (m :: Type -> Type) where
- throwM :: (HasCallStack, Exception e) => e -> m a
- throwString :: (MonadThrow m, HasCallStack) => String -> m a
- data StringException = StringException String CallStack
- class MonadThrow m => MonadCatch (m :: Type -> Type) where
- catch :: (HasCallStack, Exception e) => m a -> (e -> m a) -> m a
- catchIO :: (HasCallStack, MonadCatch m) => m a -> (IOException -> m a) -> m a
- catchIOError :: (HasCallStack, MonadCatch m) => m a -> (IOError -> m a) -> m a
- catchAny :: (HasCallStack, MonadCatch m) => m a -> (SomeException -> m a) -> m a
- catchDeep :: (HasCallStack, MonadCatch m, MonadIO m, Exception e, NFData a) => m a -> (e -> m a) -> m a
- catchAnyDeep :: (HasCallStack, MonadCatch m, MonadIO m, NFData a) => m a -> (SomeException -> m a) -> m a
- catchAsync :: (HasCallStack, MonadCatch m, Exception e) => m a -> (e -> m a) -> m a
- catchJust :: (HasCallStack, MonadCatch m, Exception e) => (e -> Maybe b) -> m a -> (b -> m a) -> m a
- handle :: (HasCallStack, MonadCatch m, Exception e) => (e -> m a) -> m a -> m a
- handleIO :: (HasCallStack, MonadCatch m) => (IOException -> m a) -> m a -> m a
- handleIOError :: (HasCallStack, MonadCatch m) => (IOError -> m a) -> m a -> m a
- handleAny :: (HasCallStack, MonadCatch m) => (SomeException -> m a) -> m a -> m a
- handleDeep :: (HasCallStack, MonadCatch m, Exception e, MonadIO m, NFData a) => (e -> m a) -> m a -> m a
- handleAnyDeep :: (HasCallStack, MonadCatch m, MonadIO m, NFData a) => (SomeException -> m a) -> m a -> m a
- handleAsync :: (HasCallStack, MonadCatch m, Exception e) => (e -> m a) -> m a -> m a
- handleJust :: (HasCallStack, MonadCatch m, Exception e) => (e -> Maybe b) -> (b -> m a) -> m a -> m a
- try :: (HasCallStack, MonadCatch m, Exception e) => m a -> m (Either e a)
- tryIO :: (HasCallStack, MonadCatch m) => m a -> m (Either IOException a)
- tryAny :: (HasCallStack, MonadCatch m) => m a -> m (Either SomeException a)
- tryDeep :: (HasCallStack, MonadCatch m, MonadIO m, Exception e, NFData a) => m a -> m (Either e a)
- tryAnyDeep :: (HasCallStack, MonadCatch m, MonadIO m, NFData a) => m a -> m (Either SomeException a)
- tryAsync :: (HasCallStack, MonadCatch m, Exception e) => m a -> m (Either e a)
- tryJust :: (HasCallStack, MonadCatch m, Exception e) => (e -> Maybe b) -> m a -> m (Either b a)
- data Handler (m :: Type -> Type) a = Exception e => Handler (e -> m a)
- catches :: (HasCallStack, MonadCatch m, MonadThrow m) => m a -> [Handler m a] -> m a
- catchesDeep :: (HasCallStack, MonadCatch m, MonadThrow m, MonadIO m, NFData a) => m a -> [Handler m a] -> m a
- catchesAsync :: (HasCallStack, MonadCatch m, MonadThrow m) => m a -> [Handler m a] -> m a
- class MonadCatch m => MonadMask (m :: Type -> Type) where
- mask :: HasCallStack => ((forall a. m a -> m a) -> m b) -> m b
- uninterruptibleMask :: HasCallStack => ((forall a. m a -> m a) -> m b) -> m b
- generalBracket :: HasCallStack => m a -> (a -> ExitCase b -> m c) -> (a -> m b) -> m (b, c)
- data ExitCase a
- onException :: (HasCallStack, MonadMask m) => m a -> m b -> m a
- bracket :: (HasCallStack, MonadMask m) => m a -> (a -> m b) -> (a -> m c) -> m c
- bracket_ :: (HasCallStack, MonadMask m) => m a -> m b -> m c -> m c
- finally :: (HasCallStack, MonadMask m) => m a -> m b -> m a
- withException :: (HasCallStack, MonadMask m, Exception e) => m a -> (e -> m b) -> m a
- bracketOnError :: (HasCallStack, MonadMask m) => m a -> (a -> m b) -> (a -> m c) -> m c
- bracketOnError_ :: (HasCallStack, MonadMask m) => m a -> m b -> m c -> m c
- bracketWithError :: (HasCallStack, MonadMask m) => m a -> (Maybe SomeException -> a -> m b) -> (a -> m c) -> m c
- data SyncExceptionWrapper = Exception e => SyncExceptionWrapper e
- toSyncException :: Exception e => e -> SomeException
- data AsyncExceptionWrapper = Exception e => AsyncExceptionWrapper e
- toAsyncException :: Exception e => e -> SomeException
- isSyncException :: Exception e => e -> Bool
- isAsyncException :: Exception e => e -> Bool
- evaluate :: a -> Eff es a
- evaluateDeep :: NFData a => a -> Eff es a
- data SomeException = Exception e => SomeException e
- class (Typeable e, Show e) => Exception e where
- toException :: e -> SomeException
- fromException :: SomeException -> Maybe e
- displayException :: e -> String
- data IOException
- data ArithException
- data ArrayException
- newtype AssertionFailed = AssertionFailed String
- newtype NoMethodError = NoMethodError String
- newtype PatternMatchFail = PatternMatchFail String
- newtype RecConError = RecConError String
- newtype RecSelError = RecSelError String
- newtype RecUpdError = RecUpdError String
- data ErrorCall where
- newtype TypeError = TypeError String
- data SomeAsyncException = Exception e => SomeAsyncException e
- data AsyncException
- asyncExceptionToException :: Exception e => e -> SomeException
- asyncExceptionFromException :: Exception e => SomeException -> Maybe e
- data NonTermination = NonTermination
- data NestedAtomically = NestedAtomically
- data BlockedIndefinitelyOnMVar = BlockedIndefinitelyOnMVar
- data BlockedIndefinitelyOnSTM = BlockedIndefinitelyOnSTM
- data AllocationLimitExceeded = AllocationLimitExceeded
- newtype CompactionFailed = CompactionFailed String
- data Deadlock = Deadlock
- assert :: Bool -> a -> a
Throwing
class Monad m => MonadThrow (m :: Type -> Type) where #
A class for monads in which exceptions may be thrown.
Instances should obey the following law:
throwM e >> x = throwM e
In other words, throwing an exception short-circuits the rest of the monadic computation.
throwM :: (HasCallStack, Exception e) => e -> m a #
Throw an exception. Note that this throws when this action is run in
the monad m
, not when it is applied. It is a generalization of
Control.Exception's throwIO
.
Should satisfy the law:
throwM e >> f = throwM e
Instances
throwString :: (MonadThrow m, HasCallStack) => String -> m a #
A convenience function for throwing a user error. This is useful for cases where it would be too high a burden to define your own exception type.
This throws an exception of type StringException
. When GHC
supports it (base 4.9 and GHC 8.0 and onward), it includes a call
stack.
Since: safe-exceptions-0.1.5.0
data StringException #
Exception type thrown by throwString
.
Note that the second field of the data constructor depends on GHC/base version. For base 4.9 and GHC 8.0 and later, the second field is a call stack. Previous versions of GHC and base do not support call stacks, and the field is simply unit (provided to make pattern matching across GHC versions easier).
Since: safe-exceptions-0.1.5.0
Instances
Exception StringException | |
Defined in Control.Exception.Safe | |
Show StringException | |
Defined in Control.Exception.Safe showsPrec :: Int -> StringException -> ShowS # show :: StringException -> String # showList :: [StringException] -> ShowS # |
Catching (with recovery)
class MonadThrow m => MonadCatch (m :: Type -> Type) where #
A class for monads which allow exceptions to be caught, in particular
exceptions which were thrown by throwM
.
Instances should obey the following law:
catch (throwM e) f = f e
Note that the ability to catch an exception does not guarantee that we can
deal with all possible exit points from a computation. Some monads, such as
continuation-based stacks, allow for more than just a success/failure
strategy, and therefore catch
cannot be used by those monads to properly
implement a function such as finally
. For more information, see
MonadMask
.
catch :: (HasCallStack, Exception e) => m a -> (e -> m a) -> m a #
Provide a handler for exceptions thrown during execution of the first
action. Note that type of the type of the argument to the handler will
constrain which exceptions are caught. See Control.Exception's
catch
.
Instances
catchIO :: (HasCallStack, MonadCatch m) => m a -> (IOException -> m a) -> m a #
catch
specialized to only catching IOException
s
Since: safe-exceptions-0.1.3.0
catchIOError :: (HasCallStack, MonadCatch m) => m a -> (IOError -> m a) -> m a #
Catch all IOError
(eqv. IOException
) exceptions. Still somewhat too
general, but better than using catchAll
. See catchIf
for an easy way
of catching specific IOError
s based on the predicates in System.IO.Error.
catchAny :: (HasCallStack, MonadCatch m) => m a -> (SomeException -> m a) -> m a #
catch
specialized to catch all synchronous exception
Since: safe-exceptions-0.1.0.0
catchDeep :: (HasCallStack, MonadCatch m, MonadIO m, Exception e, NFData a) => m a -> (e -> m a) -> m a #
Same as catch
, but fully force evaluation of the result value
to find all impure exceptions.
Since: safe-exceptions-0.1.1.0
catchAnyDeep :: (HasCallStack, MonadCatch m, MonadIO m, NFData a) => m a -> (SomeException -> m a) -> m a #
catchDeep
specialized to catch all synchronous exception
Since: safe-exceptions-0.1.1.0
catchAsync :: (HasCallStack, MonadCatch m, Exception e) => m a -> (e -> m a) -> m a #
catch
without async exception safety
Generally it's better to avoid using this function since we do not want to recover from async exceptions, see https://github.com/fpco/safe-exceptions#quickstart
Since: safe-exceptions-0.1.0.0
catchJust :: (HasCallStack, MonadCatch m, Exception e) => (e -> Maybe b) -> m a -> (b -> m a) -> m a #
handle :: (HasCallStack, MonadCatch m, Exception e) => (e -> m a) -> m a -> m a #
Flipped version of catch
Since: safe-exceptions-0.1.0.0
handleIO :: (HasCallStack, MonadCatch m) => (IOException -> m a) -> m a -> m a #
handle
specialized to only catching IOException
s
Since: safe-exceptions-0.1.3.0
handleIOError :: (HasCallStack, MonadCatch m) => (IOError -> m a) -> m a -> m a #
Flipped catchIOError
handleAny :: (HasCallStack, MonadCatch m) => (SomeException -> m a) -> m a -> m a #
Flipped version of catchAny
Since: safe-exceptions-0.1.0.0
handleDeep :: (HasCallStack, MonadCatch m, Exception e, MonadIO m, NFData a) => (e -> m a) -> m a -> m a #
Flipped version of catchDeep
Since: safe-exceptions-0.1.1.0
handleAnyDeep :: (HasCallStack, MonadCatch m, MonadIO m, NFData a) => (SomeException -> m a) -> m a -> m a #
Flipped version of catchAnyDeep
Since: safe-exceptions-0.1.1.0
handleAsync :: (HasCallStack, MonadCatch m, Exception e) => (e -> m a) -> m a -> m a #
Flipped version of catchAsync
Generally it's better to avoid using this function since we do not want to recover from async exceptions, see https://github.com/fpco/safe-exceptions#quickstart
Since: safe-exceptions-0.1.0.0
handleJust :: (HasCallStack, MonadCatch m, Exception e) => (e -> Maybe b) -> (b -> m a) -> m a -> m a #
Flipped catchJust
.
Since: safe-exceptions-0.1.4.0
try :: (HasCallStack, MonadCatch m, Exception e) => m a -> m (Either e a) #
Same as upstream try
, but will not catch asynchronous
exceptions
Since: safe-exceptions-0.1.0.0
tryIO :: (HasCallStack, MonadCatch m) => m a -> m (Either IOException a) #
try
specialized to only catching IOException
s
Since: safe-exceptions-0.1.3.0
tryAny :: (HasCallStack, MonadCatch m) => m a -> m (Either SomeException a) #
try
specialized to catch all synchronous exceptions
Since: safe-exceptions-0.1.0.0
tryDeep :: (HasCallStack, MonadCatch m, MonadIO m, Exception e, NFData a) => m a -> m (Either e a) #
Same as try
, but fully force evaluation of the result value
to find all impure exceptions.
Since: safe-exceptions-0.1.1.0
tryAnyDeep :: (HasCallStack, MonadCatch m, MonadIO m, NFData a) => m a -> m (Either SomeException a) #
tryDeep
specialized to catch all synchronous exceptions
Since: safe-exceptions-0.1.1.0
tryAsync :: (HasCallStack, MonadCatch m, Exception e) => m a -> m (Either e a) #
try
without async exception safety
Generally it's better to avoid using this function since we do not want to recover from async exceptions, see https://github.com/fpco/safe-exceptions#quickstart
Since: safe-exceptions-0.1.0.0
tryJust :: (HasCallStack, MonadCatch m, Exception e) => (e -> Maybe b) -> m a -> m (Either b a) #
A variant of try
that takes an exception predicate to select
which exceptions are caught.
Since: safe-exceptions-0.1.4.0
catches :: (HasCallStack, MonadCatch m, MonadThrow m) => m a -> [Handler m a] -> m a #
Same as upstream catches
, but will not catch asynchronous
exceptions
Since: safe-exceptions-0.1.2.0
catchesDeep :: (HasCallStack, MonadCatch m, MonadThrow m, MonadIO m, NFData a) => m a -> [Handler m a] -> m a #
Same as catches
, but fully force evaluation of the result value
to find all impure exceptions.
Since: safe-exceptions-0.1.2.0
catchesAsync :: (HasCallStack, MonadCatch m, MonadThrow m) => m a -> [Handler m a] -> m a #
catches
without async exception safety
Generally it's better to avoid using this function since we do not want to recover from async exceptions, see https://github.com/fpco/safe-exceptions#quickstart
Since: safe-exceptions-0.1.2.0
Cleanup (no recovery)
class MonadCatch m => MonadMask (m :: Type -> Type) where #
A class for monads which provide for the ability to account for all possible exit points from a computation, and to mask asynchronous exceptions. Continuation-based monads are invalid instances of this class.
Instances should ensure that, in the following code:
fg = f `finally` g
The action g
is called regardless of what occurs within f
, including
async exceptions. Some monads allow f
to abort the computation via other
effects than throwing an exception. For simplicity, we will consider aborting
and throwing an exception to be two forms of "throwing an error".
If f
and g
both throw an error, the error thrown by fg
depends on which
errors we're talking about. In a monad transformer stack, the deeper layers
override the effects of the inner layers; for example, ExceptT e1 (Except
e2) a
represents a value of type Either e2 (Either e1 a)
, so throwing both
an e1
and an e2
will result in Left e2
. If f
and g
both throw an
error from the same layer, instances should ensure that the error from g
wins.
Effects other than throwing an error are also overridden by the deeper layers.
For example, StateT s Maybe a
represents a value of type s -> Maybe (a,
s)
, so if an error thrown from f
causes this function to return Nothing
,
any changes to the state which f
also performed will be erased. As a
result, g
will see the state as it was before f
. Once g
completes,
f
's error will be rethrown, so g
' state changes will be erased as well.
This is the normal interaction between effects in a monad transformer stack.
By contrast, lifted-base's
version of finally
always discards all of g
's non-IO effects, and g
never sees any of f
's non-IO effects, regardless of the layer ordering and
regardless of whether f
throws an error. This is not the result of
interacting effects, but a consequence of MonadBaseControl
's approach.
mask :: HasCallStack => ((forall a. m a -> m a) -> m b) -> m b #
Runs an action with asynchronous exceptions disabled. The action is
provided a method for restoring the async. environment to what it was
at the mask
call. See Control.Exception's mask
.
uninterruptibleMask :: HasCallStack => ((forall a. m a -> m a) -> m b) -> m b #
Like mask
, but the masked computation is not interruptible (see
Control.Exception's uninterruptibleMask
. WARNING:
Only use if you need to mask exceptions around an interruptible operation
AND you can guarantee the interruptible operation will only block for a
short period of time. Otherwise you render the program/thread unresponsive
and/or unkillable.
:: HasCallStack | |
=> m a | acquire some resource |
-> (a -> ExitCase b -> m c) | release the resource, observing the outcome of the inner action |
-> (a -> m b) | inner action to perform with the resource |
-> m (b, c) |
A generalized version of bracket
which uses ExitCase
to distinguish
the different exit cases, and returns the values of both the use
and
release
actions. In practice, this extra information is rarely needed,
so it is often more convenient to use one of the simpler functions which
are defined in terms of this one, such as bracket
, finally
, onError
,
and bracketOnError
.
This function exists because in order to thread their effects through the
execution of bracket
, monad transformers need values to be threaded from
use
to release
and from release
to the output value.
NOTE This method was added in version 0.9.0 of this
library. Previously, implementation of functions like bracket
and finally
in this module were based on the mask
and
uninterruptibleMask
functions only, disallowing some classes of
tranformers from having MonadMask
instances (notably
multi-exit-point transformers like ExceptT
). If you are a
library author, you'll now need to provide an implementation for
this method. The StateT
implementation demonstrates most of the
subtleties:
generalBracket acquire release use = StateT $ s0 -> do ((b, _s2), (c, s3)) <- generalBracket (runStateT acquire s0) ((resource, s1) exitCase -> case exitCase of ExitCaseSuccess (b, s2) -> runStateT (release resource (ExitCaseSuccess b)) s2 -- In the two other cases, the base monad overridesuse
's state -- changes and the state reverts tos1
. ExitCaseException e -> runStateT (release resource (ExitCaseException e)) s1 ExitCaseAbort -> runStateT (release resource ExitCaseAbort) s1 ) ((resource, s1) -> runStateT (use resource) s1) return ((b, c), s3)
The StateT s m
implementation of generalBracket
delegates to the m
implementation of generalBracket
. The acquire
, use
, and release
arguments given to StateT
's implementation produce actions of type
StateT s m a
, StateT s m b
, and StateT s m c
. In order to run those
actions in the base monad, we need to call runStateT
, from which we
obtain actions of type m (a, s)
, m (b, s)
, and m (c, s)
. Since each
action produces the next state, it is important to feed the state produced
by the previous action to the next action.
In the ExitCaseSuccess
case, the state starts at s0
, flows through
acquire
to become s1
, flows through use
to become s2
, and finally
flows through release
to become s3
. In the other two cases, release
does not receive the value s2
, so its action cannot see the state changes
performed by use
. This is fine, because in those two cases, an error was
thrown in the base monad, so as per the usual interaction between effects
in a monad transformer stack, those state changes get reverted. So we start
from s1
instead.
Finally, the m
implementation of generalBracket
returns the pairs
(b, s)
and (c, s)
. For monad transformers other than StateT
, this
will be some other type representing the effects and values performed and
returned by the use
and release
actions. The effect part of the use
result, in this case _s2
, usually needs to be discarded, since those
effects have already been incorporated in the release
action.
The only effect which is intentionally not incorporated in the release
action is the effect of throwing an error. In that case, the error must be
re-thrown. One subtlety which is easy to miss is that in the case in which
use
and release
both throw an error, the error from release
should
take priority. Here is an implementation for ExceptT
which demonstrates
how to do this.
generalBracket acquire release use = ExceptT $ do (eb, ec) <- generalBracket (runExceptT acquire) (eresource exitCase -> case eresource of Left e -> return (Left e) -- nothing to release, acquire didn't succeed Right resource -> case exitCase of ExitCaseSuccess (Right b) -> runExceptT (release resource (ExitCaseSuccess b)) ExitCaseException e -> runExceptT (release resource (ExitCaseException e)) _ -> runExceptT (release resource ExitCaseAbort)) (either (return . Left) (runExceptT . use)) return $ do -- The order in which we perform those twoEither
effects determines -- which error will win if they are bothLeft
s. We want the error from --release
to win. c <- ec b <- eb return (b, c)
Since: exceptions-0.9.0
Instances
MonadMask IO | |
Defined in Control.Monad.Catch mask :: HasCallStack => ((forall a. IO a -> IO a) -> IO b) -> IO b # uninterruptibleMask :: HasCallStack => ((forall a. IO a -> IO a) -> IO b) -> IO b # generalBracket :: HasCallStack => IO a -> (a -> ExitCase b -> IO c) -> (a -> IO b) -> IO (b, c) # | |
e ~ SomeException => MonadMask (Either e) | Since: exceptions-0.8.3 |
Defined in Control.Monad.Catch mask :: HasCallStack => ((forall a. Either e a -> Either e a) -> Either e b) -> Either e b # uninterruptibleMask :: HasCallStack => ((forall a. Either e a -> Either e a) -> Either e b) -> Either e b # generalBracket :: HasCallStack => Either e a -> (a -> ExitCase b -> Either e c) -> (a -> Either e b) -> Either e (b, c) # | |
MonadMask (Eff es) Source # | |
Defined in Effectful.Internal.Monad mask :: HasCallStack => ((forall a. Eff es a -> Eff es a) -> Eff es b) -> Eff es b # uninterruptibleMask :: HasCallStack => ((forall a. Eff es a -> Eff es a) -> Eff es b) -> Eff es b # generalBracket :: HasCallStack => Eff es a -> (a -> ExitCase b -> Eff es c) -> (a -> Eff es b) -> Eff es (b, c) # | |
MonadMask m => MonadMask (MaybeT m) | Since: exceptions-0.10.0 |
Defined in Control.Monad.Catch mask :: HasCallStack => ((forall a. MaybeT m a -> MaybeT m a) -> MaybeT m b) -> MaybeT m b # uninterruptibleMask :: HasCallStack => ((forall a. MaybeT m a -> MaybeT m a) -> MaybeT m b) -> MaybeT m b # generalBracket :: HasCallStack => MaybeT m a -> (a -> ExitCase b -> MaybeT m c) -> (a -> MaybeT m b) -> MaybeT m (b, c) # | |
MonadMask m => MonadMask (ExceptT e m) | Since: exceptions-0.9.0 |
Defined in Control.Monad.Catch mask :: HasCallStack => ((forall a. ExceptT e m a -> ExceptT e m a) -> ExceptT e m b) -> ExceptT e m b # uninterruptibleMask :: HasCallStack => ((forall a. ExceptT e m a -> ExceptT e m a) -> ExceptT e m b) -> ExceptT e m b # generalBracket :: HasCallStack => ExceptT e m a -> (a -> ExitCase b -> ExceptT e m c) -> (a -> ExceptT e m b) -> ExceptT e m (b, c) # | |
MonadMask m => MonadMask (IdentityT m) | |
Defined in Control.Monad.Catch mask :: HasCallStack => ((forall a. IdentityT m a -> IdentityT m a) -> IdentityT m b) -> IdentityT m b # uninterruptibleMask :: HasCallStack => ((forall a. IdentityT m a -> IdentityT m a) -> IdentityT m b) -> IdentityT m b # generalBracket :: HasCallStack => IdentityT m a -> (a -> ExitCase b -> IdentityT m c) -> (a -> IdentityT m b) -> IdentityT m (b, c) # | |
MonadMask m => MonadMask (ReaderT r m) | |
Defined in Control.Monad.Catch mask :: HasCallStack => ((forall a. ReaderT r m a -> ReaderT r m a) -> ReaderT r m b) -> ReaderT r m b # uninterruptibleMask :: HasCallStack => ((forall a. ReaderT r m a -> ReaderT r m a) -> ReaderT r m b) -> ReaderT r m b # generalBracket :: HasCallStack => ReaderT r m a -> (a -> ExitCase b -> ReaderT r m c) -> (a -> ReaderT r m b) -> ReaderT r m (b, c) # | |
MonadMask m => MonadMask (StateT s m) | |
Defined in Control.Monad.Catch mask :: HasCallStack => ((forall a. StateT s m a -> StateT s m a) -> StateT s m b) -> StateT s m b # uninterruptibleMask :: HasCallStack => ((forall a. StateT s m a -> StateT s m a) -> StateT s m b) -> StateT s m b # generalBracket :: HasCallStack => StateT s m a -> (a -> ExitCase b -> StateT s m c) -> (a -> StateT s m b) -> StateT s m (b, c) # | |
MonadMask m => MonadMask (StateT s m) | |
Defined in Control.Monad.Catch mask :: HasCallStack => ((forall a. StateT s m a -> StateT s m a) -> StateT s m b) -> StateT s m b # uninterruptibleMask :: HasCallStack => ((forall a. StateT s m a -> StateT s m a) -> StateT s m b) -> StateT s m b # generalBracket :: HasCallStack => StateT s m a -> (a -> ExitCase b -> StateT s m c) -> (a -> StateT s m b) -> StateT s m (b, c) # | |
(MonadMask m, Monoid w) => MonadMask (WriterT w m) | |
Defined in Control.Monad.Catch mask :: HasCallStack => ((forall a. WriterT w m a -> WriterT w m a) -> WriterT w m b) -> WriterT w m b # uninterruptibleMask :: HasCallStack => ((forall a. WriterT w m a -> WriterT w m a) -> WriterT w m b) -> WriterT w m b # generalBracket :: HasCallStack => WriterT w m a -> (a -> ExitCase b -> WriterT w m c) -> (a -> WriterT w m b) -> WriterT w m (b, c) # | |
(MonadMask m, Monoid w) => MonadMask (WriterT w m) | |
Defined in Control.Monad.Catch mask :: HasCallStack => ((forall a. WriterT w m a -> WriterT w m a) -> WriterT w m b) -> WriterT w m b # uninterruptibleMask :: HasCallStack => ((forall a. WriterT w m a -> WriterT w m a) -> WriterT w m b) -> WriterT w m b # generalBracket :: HasCallStack => WriterT w m a -> (a -> ExitCase b -> WriterT w m c) -> (a -> WriterT w m b) -> WriterT w m (b, c) # | |
(MonadMask m, Monoid w) => MonadMask (RWST r w s m) | |
Defined in Control.Monad.Catch mask :: HasCallStack => ((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 # uninterruptibleMask :: HasCallStack => ((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 # generalBracket :: HasCallStack => RWST r w s m a -> (a -> ExitCase b -> RWST r w s m c) -> (a -> RWST r w s m b) -> RWST r w s m (b, c) # | |
(MonadMask m, Monoid w) => MonadMask (RWST r w s m) | |
Defined in Control.Monad.Catch mask :: HasCallStack => ((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 # uninterruptibleMask :: HasCallStack => ((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 # generalBracket :: HasCallStack => RWST r w s m a -> (a -> ExitCase b -> RWST r w s m c) -> (a -> RWST r w s m b) -> RWST r w s m (b, c) # |
A MonadMask
computation may either succeed with a value, abort with an
exception, or abort for some other reason. For example, in ExceptT e IO
you can use throwM
to abort with an exception (ExitCaseException
) or
throwE
to abort with a value of type e
(ExitCaseAbort
).
onException :: (HasCallStack, MonadMask m) => m a -> m b -> m a #
Async safe version of onException
Since: safe-exceptions-0.1.0.0
bracket :: (HasCallStack, MonadMask m) => m a -> (a -> m b) -> (a -> m c) -> m c #
Async safe version of bracket
Since: safe-exceptions-0.1.0.0
bracket_ :: (HasCallStack, MonadMask m) => m a -> m b -> m c -> m c #
Async safe version of bracket_
Since: safe-exceptions-0.1.0.0
finally :: (HasCallStack, MonadMask m) => m a -> m b -> m a #
Async safe version of finally
Since: safe-exceptions-0.1.0.0
withException :: (HasCallStack, MonadMask m, Exception e) => m a -> (e -> m b) -> m a #
Like onException
, but provides the handler the thrown
exception.
Since: safe-exceptions-0.1.0.0
bracketOnError :: (HasCallStack, MonadMask m) => m a -> (a -> m b) -> (a -> m c) -> m c #
Async safe version of bracketOnError
Since: safe-exceptions-0.1.0.0
bracketOnError_ :: (HasCallStack, MonadMask m) => m a -> m b -> m c -> m c #
A variant of bracketOnError
where the return value from the first
computation is not required.
Since: safe-exceptions-0.1.0.0
bracketWithError :: (HasCallStack, MonadMask m) => m a -> (Maybe SomeException -> a -> m b) -> (a -> m c) -> m c #
Async safe version of bracket
with access to the exception in the
cleanup action.
Since: safe-exceptions-0.1.7.0
Utilities
Coercion to sync and async
data SyncExceptionWrapper #
Wrap up an asynchronous exception to be treated as a synchronous exception
This is intended to be created via toSyncException
Since: safe-exceptions-0.1.0.0
Exception e => SyncExceptionWrapper e |
Instances
Exception SyncExceptionWrapper | |
Show SyncExceptionWrapper | |
Defined in Control.Exception.Safe showsPrec :: Int -> SyncExceptionWrapper -> ShowS # show :: SyncExceptionWrapper -> String # showList :: [SyncExceptionWrapper] -> ShowS # |
toSyncException :: Exception e => e -> SomeException #
Convert an exception into a synchronous exception
For synchronous exceptions, this is the same as toException
.
For asynchronous exceptions, this will wrap up the exception with
SyncExceptionWrapper
Since: safe-exceptions-0.1.0.0
data AsyncExceptionWrapper #
Wrap up a synchronous exception to be treated as an asynchronous exception
This is intended to be created via toAsyncException
Since: safe-exceptions-0.1.0.0
Exception e => AsyncExceptionWrapper e |
Instances
Exception AsyncExceptionWrapper | |
Show AsyncExceptionWrapper | |
Defined in Control.Exception.Safe showsPrec :: Int -> AsyncExceptionWrapper -> ShowS # show :: AsyncExceptionWrapper -> String # showList :: [AsyncExceptionWrapper] -> ShowS # |
toAsyncException :: Exception e => e -> SomeException #
Convert an exception into an asynchronous exception
For asynchronous exceptions, this is the same as toException
.
For synchronous exceptions, this will wrap up the exception with
AsyncExceptionWrapper
Since: safe-exceptions-0.1.0.0
Check exception type
isSyncException :: Exception e => e -> Bool #
Check if the given exception is synchronous
Since: safe-exceptions-0.1.0.0
isAsyncException :: Exception e => e -> Bool #
Check if the given exception is asynchronous
Since: safe-exceptions-0.1.0.0
Evaluation
evaluateDeep :: NFData a => a -> Eff es a Source #
Re-exports from Control.Exception
The SomeException
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
.
Exception e => SomeException e |
Instances
Exception SomeException | Since: base-3.0 |
Defined in GHC.Exception.Type | |
Show SomeException | Since: base-3.0 |
Defined in GHC.Exception.Type showsPrec :: Int -> SomeException -> ShowS # show :: SomeException -> String # showList :: [SomeException] -> ShowS # |
The Exception
class
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
Nothing
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
Concrete exception types
data IOException #
Exceptions that occur in the IO
monad.
An IOException
records a more specific error type, a descriptive
string and maybe the handle that was used when the error was
flagged.
Instances
Exception IOException | Since: base-4.1.0.0 |
Defined in GHC.IO.Exception | |
Show IOException | Since: base-4.1.0.0 |
Defined in GHC.IO.Exception showsPrec :: Int -> IOException -> ShowS # show :: IOException -> String # showList :: [IOException] -> ShowS # | |
Eq IOException | Since: base-4.1.0.0 |
Defined in GHC.IO.Exception (==) :: IOException -> IOException -> Bool # (/=) :: IOException -> IOException -> Bool # |
data ArithException #
Arithmetic exceptions.
Overflow | |
Underflow | |
LossOfPrecision | |
DivideByZero | |
Denormal | |
RatioZeroDenominator | Since: base-4.6.0.0 |
Instances
Exception ArithException | Since: base-4.0.0.0 |
Defined in GHC.Exception.Type | |
Show ArithException | Since: base-4.0.0.0 |
Defined in GHC.Exception.Type showsPrec :: Int -> ArithException -> ShowS # show :: ArithException -> String # showList :: [ArithException] -> ShowS # | |
Eq ArithException | Since: base-3.0 |
Defined in GHC.Exception.Type (==) :: ArithException -> ArithException -> Bool # (/=) :: ArithException -> ArithException -> Bool # | |
Ord ArithException | Since: base-3.0 |
Defined in GHC.Exception.Type compare :: ArithException -> ArithException -> Ordering # (<) :: ArithException -> ArithException -> Bool # (<=) :: ArithException -> ArithException -> Bool # (>) :: ArithException -> ArithException -> Bool # (>=) :: ArithException -> ArithException -> Bool # max :: ArithException -> ArithException -> ArithException # min :: ArithException -> ArithException -> ArithException # |
data ArrayException #
Exceptions generated by array operations
IndexOutOfBounds String | An attempt was made to index an array outside its declared bounds. |
UndefinedElement String | An attempt was made to evaluate an element of an array that had not been initialized. |
Instances
Exception ArrayException | Since: base-4.1.0.0 |
Defined in GHC.IO.Exception | |
Show ArrayException | Since: base-4.1.0.0 |
Defined in GHC.IO.Exception showsPrec :: Int -> ArrayException -> ShowS # show :: ArrayException -> String # showList :: [ArrayException] -> ShowS # | |
Eq ArrayException | Since: base-4.2.0.0 |
Defined in GHC.IO.Exception (==) :: ArrayException -> ArrayException -> Bool # (/=) :: ArrayException -> ArrayException -> Bool # | |
Ord ArrayException | Since: base-4.2.0.0 |
Defined in GHC.IO.Exception compare :: ArrayException -> ArrayException -> Ordering # (<) :: ArrayException -> ArrayException -> Bool # (<=) :: ArrayException -> ArrayException -> Bool # (>) :: ArrayException -> ArrayException -> Bool # (>=) :: ArrayException -> ArrayException -> Bool # max :: ArrayException -> ArrayException -> ArrayException # min :: ArrayException -> ArrayException -> ArrayException # |
newtype AssertionFailed #
Instances
Exception AssertionFailed | Since: base-4.1.0.0 |
Defined in GHC.IO.Exception | |
Show AssertionFailed | Since: base-4.1.0.0 |
Defined in GHC.IO.Exception showsPrec :: Int -> AssertionFailed -> ShowS # show :: AssertionFailed -> String # showList :: [AssertionFailed] -> ShowS # |
newtype NoMethodError #
A class method without a definition (neither a default definition,
nor a definition in the appropriate instance) was called. The
String
gives information about which method it was.
Instances
Exception NoMethodError | Since: base-4.0 |
Defined in Control.Exception.Base | |
Show NoMethodError | Since: base-4.0 |
Defined in Control.Exception.Base showsPrec :: Int -> NoMethodError -> ShowS # show :: NoMethodError -> String # showList :: [NoMethodError] -> ShowS # |
newtype PatternMatchFail #
A pattern match failed. The String
gives information about the
source location of the pattern.
Instances
Exception PatternMatchFail | Since: base-4.0 |
Defined in Control.Exception.Base | |
Show PatternMatchFail | Since: base-4.0 |
Defined in Control.Exception.Base showsPrec :: Int -> PatternMatchFail -> ShowS # show :: PatternMatchFail -> String # showList :: [PatternMatchFail] -> ShowS # |
newtype RecConError #
An uninitialised record field was used. The String
gives
information about the source location where the record was
constructed.
Instances
Exception RecConError | Since: base-4.0 |
Defined in Control.Exception.Base | |
Show RecConError | Since: base-4.0 |
Defined in Control.Exception.Base showsPrec :: Int -> RecConError -> ShowS # show :: RecConError -> String # showList :: [RecConError] -> ShowS # |
newtype RecSelError #
A record selector was applied to a constructor without the
appropriate field. This can only happen with a datatype with
multiple constructors, where some fields are in one constructor
but not another. The String
gives information about the source
location of the record selector.
Instances
Exception RecSelError | Since: base-4.0 |
Defined in Control.Exception.Base | |
Show RecSelError | Since: base-4.0 |
Defined in Control.Exception.Base showsPrec :: Int -> RecSelError -> ShowS # show :: RecSelError -> String # showList :: [RecSelError] -> ShowS # |
newtype RecUpdError #
A record update was performed on a constructor without the
appropriate field. This can only happen with a datatype with
multiple constructors, where some fields are in one constructor
but not another. The String
gives information about the source
location of the record update.
Instances
Exception RecUpdError | Since: base-4.0 |
Defined in Control.Exception.Base | |
Show RecUpdError | Since: base-4.0 |
Defined in Control.Exception.Base showsPrec :: Int -> RecUpdError -> ShowS # show :: RecUpdError -> String # showList :: [RecUpdError] -> ShowS # |
This is thrown when the user calls error
. The first String
is the
argument given to error
, second String
is the location.
Instances
Exception ErrorCall | Since: base-4.0.0.0 |
Defined in GHC.Exception toException :: ErrorCall -> SomeException # fromException :: SomeException -> Maybe ErrorCall # displayException :: ErrorCall -> String # | |
Show ErrorCall | Since: base-4.0.0.0 |
Eq ErrorCall | Since: base-4.7.0.0 |
Ord ErrorCall | Since: base-4.7.0.0 |
Defined in GHC.Exception |
An expression that didn't typecheck during compile time was called.
This is only possible with -fdefer-type-errors. The String
gives
details about the failed type check.
Since: base-4.9.0.0
Instances
Exception TypeError | Since: base-4.9.0.0 |
Defined in Control.Exception.Base toException :: TypeError -> SomeException # fromException :: SomeException -> Maybe TypeError # displayException :: TypeError -> String # | |
Show TypeError | Since: base-4.9.0.0 |
Asynchronous exceptions
data SomeAsyncException #
Superclass for asynchronous exceptions.
Since: base-4.7.0.0
Exception e => SomeAsyncException e |
Instances
Exception SomeAsyncException | Since: base-4.7.0.0 |
Defined in GHC.IO.Exception | |
Show SomeAsyncException | Since: base-4.7.0.0 |
Defined in GHC.IO.Exception showsPrec :: Int -> SomeAsyncException -> ShowS # show :: SomeAsyncException -> String # showList :: [SomeAsyncException] -> ShowS # |
data AsyncException #
Asynchronous exceptions.
StackOverflow | The current thread's stack exceeded its limit. Since an exception has been raised, the thread's stack will certainly be below its limit again, but the programmer should take remedial action immediately. |
HeapOverflow | The program's heap is reaching its limit, and the program should take action to reduce the amount of live data it has. Notes:
|
ThreadKilled | This exception is raised by another thread
calling |
UserInterrupt | This exception is raised by default in the main thread of the program when the user requests to terminate the program via the usual mechanism(s) (e.g. Control-C in the console). |
Instances
Exception AsyncException | Since: base-4.7.0.0 |
Defined in GHC.IO.Exception | |
Show AsyncException | Since: base-4.1.0.0 |
Defined in GHC.IO.Exception showsPrec :: Int -> AsyncException -> ShowS # show :: AsyncException -> String # showList :: [AsyncException] -> ShowS # | |
Eq AsyncException | Since: base-4.2.0.0 |
Defined in GHC.IO.Exception (==) :: AsyncException -> AsyncException -> Bool # (/=) :: AsyncException -> AsyncException -> Bool # | |
Ord AsyncException | Since: base-4.2.0.0 |
Defined in GHC.IO.Exception compare :: AsyncException -> AsyncException -> Ordering # (<) :: AsyncException -> AsyncException -> Bool # (<=) :: AsyncException -> AsyncException -> Bool # (>) :: AsyncException -> AsyncException -> Bool # (>=) :: AsyncException -> AsyncException -> Bool # max :: AsyncException -> AsyncException -> AsyncException # min :: AsyncException -> AsyncException -> AsyncException # |
asyncExceptionToException :: Exception e => e -> SomeException #
Since: base-4.7.0.0
asyncExceptionFromException :: Exception e => SomeException -> Maybe e #
Since: base-4.7.0.0
data NonTermination #
Thrown when the runtime system detects that the computation is guaranteed not to terminate. Note that there is no guarantee that the runtime system will notice whether any given computation is guaranteed to terminate or not.
Instances
Exception NonTermination | Since: base-4.0 |
Defined in Control.Exception.Base | |
Show NonTermination | Since: base-4.0 |
Defined in Control.Exception.Base showsPrec :: Int -> NonTermination -> ShowS # show :: NonTermination -> String # showList :: [NonTermination] -> ShowS # |
data NestedAtomically #
Thrown when the program attempts to call atomically
, from the stm
package, inside another call to atomically
.
Instances
Exception NestedAtomically | Since: base-4.0 |
Defined in Control.Exception.Base | |
Show NestedAtomically | Since: base-4.0 |
Defined in Control.Exception.Base showsPrec :: Int -> NestedAtomically -> ShowS # show :: NestedAtomically -> String # showList :: [NestedAtomically] -> ShowS # |
data BlockedIndefinitelyOnMVar #
The thread is blocked on an MVar
, but there are no other references
to the MVar
so it can't ever continue.
Instances
Exception BlockedIndefinitelyOnMVar | Since: base-4.1.0.0 |
Show BlockedIndefinitelyOnMVar | Since: base-4.1.0.0 |
Defined in GHC.IO.Exception showsPrec :: Int -> BlockedIndefinitelyOnMVar -> ShowS # show :: BlockedIndefinitelyOnMVar -> String # showList :: [BlockedIndefinitelyOnMVar] -> ShowS # |
data BlockedIndefinitelyOnSTM #
The thread is waiting to retry an STM transaction, but there are no
other references to any TVar
s involved, so it can't ever continue.
Instances
Exception BlockedIndefinitelyOnSTM | Since: base-4.1.0.0 |
Show BlockedIndefinitelyOnSTM | Since: base-4.1.0.0 |
Defined in GHC.IO.Exception showsPrec :: Int -> BlockedIndefinitelyOnSTM -> ShowS # show :: BlockedIndefinitelyOnSTM -> String # showList :: [BlockedIndefinitelyOnSTM] -> ShowS # |
data AllocationLimitExceeded #
This thread has exceeded its allocation limit. See
setAllocationCounter
and
enableAllocationLimit
.
Since: base-4.8.0.0
Instances
Exception AllocationLimitExceeded | Since: base-4.8.0.0 |
Show AllocationLimitExceeded | Since: base-4.7.1.0 |
Defined in GHC.IO.Exception showsPrec :: Int -> AllocationLimitExceeded -> ShowS # show :: AllocationLimitExceeded -> String # showList :: [AllocationLimitExceeded] -> ShowS # |
newtype CompactionFailed #
Compaction found an object that cannot be compacted. Functions
cannot be compacted, nor can mutable objects or pinned objects.
See compact
.
Since: base-4.10.0.0
Instances
Exception CompactionFailed | Since: base-4.10.0.0 |
Defined in GHC.IO.Exception | |
Show CompactionFailed | Since: base-4.10.0.0 |
Defined in GHC.IO.Exception showsPrec :: Int -> CompactionFailed -> ShowS # show :: CompactionFailed -> String # showList :: [CompactionFailed] -> ShowS # |
There are no runnable threads, so the program is deadlocked.
The Deadlock
exception is raised in the main thread only.
Instances
Exception Deadlock | Since: base-4.1.0.0 |
Defined in GHC.IO.Exception toException :: Deadlock -> SomeException # fromException :: SomeException -> Maybe Deadlock # displayException :: Deadlock -> String # | |
Show Deadlock | Since: base-4.1.0.0 |
Assertions
If the first argument evaluates to True
, then the result is the
second argument. Otherwise an AssertionFailed
exception
is raised, containing a String
with the source file and line number of the
call to assert
.
Assertions can normally be turned on or off with a compiler flag
(for GHC, assertions are normally on unless optimisation is turned on
with -O
or the -fignore-asserts
option is given). When assertions are turned off, the first
argument to assert
is ignored, and the second argument is
returned as the result.