Lifted throwIO.
Moduleeffectful-core-2.7.1.2GHC2021
Effectful.Exception
Support for runtime exceptions.
This module supplies thin wrappers over functions from Control.Exception as well as several utility functions for convenience.
Note: the Eff monad provides instances for MonadThrow, MonadCatch and MonadMask, so any existing code that uses them remains compatible.
- 28 types
- 2 classes
- 57 values
- Packageeffectful-core-2.7.1.2
- Exports88
- LanguageGHC2021
- LicenceBSD-3-Clause
- SourceException.hs
Throwing
2 declarationsLifted rethrowIO.
Catching (with recovery)
26 declarationsNote: do not use catch, handle or try to catch SomeException unless you're really sure you want to catch all exceptions (including asynchronous ones). Instead:
If you want to catch all exceptions, run a cleanup action and rethrow, use one of the functions from the cleanup section.
If you want to catch all synchronous exceptions, use catchSync, handleSync or trySync.
Lifted catch.
catchNoPropagate :: Exception e=> Eff es a-> (ExceptionWithContext e -> Eff es a)The exception handler.
-> Eff es a
Lifted catchNoPropagate.
catchDeep A variant of catch that fully forces evaluation of the result value to find all impure exceptions.
catchJust Lifted catchJust.
catchIf Catch an exception only if it satisfies a specific predicate.
catch specialized to catch IOException.
catch specialized to catch all exceptions considered to be synchronous.
catchSync ≡ catchIf @SomeException isSyncExceptionSee the check exception type section for more information.
catchSyncDeep :: NFData a=> Eff es a-> (SomeException -> Eff es a)The exception handler.
-> Eff es a
A variant of catchSync that fully forces evaluation of the result value to find all impure exceptions.
Flipped version of catch.
handleDeep Flipped version of catchDeep.
handleJust Flipped version of catchJust.
handleIf Flipped version of catchIf.
Flipped version of catchIO.
Flipped version of catchSync.
handleSyncDeep :: NFData a=> (SomeException -> Eff es a)The exception handler.
-> Eff es a-> Eff es a
Flipped version of catchSyncDeep.
Lifted try.
Lifted tryWithContext.
A variant of try that fully forces evaluation of the result value to find all impure exceptions.
Lifted tryJust.
Catch an exception only if it satisfies a specific predicate.
try specialized to catch IOException.
try specialized to catch all exceptions considered to be synchronous.
trySync ≡ tryIf @SomeException isSyncExceptionSee the check exception type section for more information.
A variant of trySync that fully forces evaluation of the result value to find all impure exceptions.
Lifted catches.
A variant of catches that fully forces evaluation of the result value to find all impure exceptions.
Cleanup (no recovery)
8 declarationsNote: when compiled with base >= 4.21, if the computation to run last
throws an exception while another one is being propagated, the original
exception is preserved in a WhileHandling annotation of the new one. This
is what the corresponding functions from Control.Exception do since base
4.23 instead of discarding the original exception, see CLC proposal
#397.
bracket Lifted bracket.
bracket_ Lifted bracket_.
bracketOnError Lifted bracketOnError.
generalBracket Generalization of bracket.
See generalBracket for more information.
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).
Constructors
Lifted finally.
onException Lifted onException.
withException A variant of onException that gives access to the exception.
Utils
0 declarationsEvaluation
Lifted evaluate.
Annotations
Lifted annotateIO.
Check exception type
Note: there's no way to determine whether an exception was thrown synchronously or asynchronously, so these functions rely on a heuristic. Namely, an exception type is determined by its Exception instance.
Exception types with the default Exception instance are considered synchronous:
data SyncEx = SyncEx deriving (Show)instance Exception SyncEx
isSyncException SyncExTrue
isAsyncException SyncExFalse
Whereas for asynchronous exceptions you need to define their Exception instance as follows:
data AsyncEx = AsyncEx deriving (Show):{ instance Exception AsyncEx where toException = asyncExceptionToException fromException = asyncExceptionFromException:}
isSyncException AsyncExFalse
isAsyncException AsyncExTrue
Check if the given exception is considered synchronous.
Check if the given exception is considered asynchronous.
Low-level API
8 declarationsLifted mask.
Lifted mask_.
Lifted uninterruptibleMask.
Lifted uninterruptibleMask_.
Describes the behaviour of a thread when an asynchronous exception is received.
Constructors
Unmaskedasynchronous exceptions are unmasked (the normal state)
MaskedInterruptiblethe state during mask: asynchronous exceptions are masked, but blocking operations may still be interrupted
MaskedUninterruptiblethe state during uninterruptibleMask: asynchronous exceptions are masked, and blocking operations may not be interrupted
Instances3Eq, Show, NFData
Eq MaskingStateDefined in ghc-internal-9.1401.0 · GHC.Internal.IOShow MaskingStateDefined in ghc-internal-9.1401.0 · GHC.Internal.IONFData MaskingStateDefined in deepseq-1.5.1.0 · Control.DeepSeq
Lifted getMaskingState.
Lifted interruptible.
Lifted allowInterrupt.
Re-exports from Control.Exception
0 declarationsThe SomeException type
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.
Constructors
forall e. (Exception e, HasExceptionContext) => SomeException e
Instances2Show, Exception
Show SomeExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.Exception.TypeException SomeExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.Exception.TypeThis drops any attached ExceptionContext.
The Exception class
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 MyExceptionThe 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 = frontendExceptionFromExceptionWe 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 -> SomeExceptiontoExceptionshould produce a SomeException with no attached ExceptionContext.fromException :: SomeException -> Maybe edisplayException :: e -> StringRender this exception value in a human-friendly manner.
Default implementation:
show.backtraceDesired :: e -> Bool
Instances32Exception, …
Exception TimeoutDefined in base-4.22.0.0 · System.TimeoutException ErrorWrapperDefined in effectful-core-2.7.1.2 · Effectful.Error.StaticException ReturnWithWrapperDefined in effectful-core-2.7.1.2 · Effectful.ReturnWith.StaticException VoidDefined in ghc-internal-9.1401.0 · GHC.Internal.Exception.TypeException NestedAtomicallyDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Exception.BaseException NoMatchingContinuationPromptDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Exception.BaseException NoMethodErrorDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Exception.BaseException NonTerminationDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Exception.BaseException PatternMatchFailDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Exception.BaseException RecConErrorDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Exception.BaseException RecSelErrorDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Exception.BaseException RecUpdErrorDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Exception.BaseException TypeErrorDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Exception.BaseException DynamicDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.DynamicException ErrorCallDefined in ghc-internal-9.1401.0 · GHC.Internal.ExceptionException ArithExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.Exception.TypeException SomeExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.Exception.TypeThis drops any attached ExceptionContext.
Exception AllocationLimitExceededDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionException ArrayExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionException AssertionFailedDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionException AsyncExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionException BlockedIndefinitelyOnMVarDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionException BlockedIndefinitelyOnSTMDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionException CompactionFailedDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionException DeadlockDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionException ExitCodeDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionException FixIOExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionException IOExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionException SomeAsyncExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionException FileLockingNotSupportedDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.Handle.Lock.CommonException a => Exception (ExceptionWithContext a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Exception.TypeException e => Exception (NoBacktrace e)Defined in ghc-internal-9.1401.0 · GHC.Internal.Exception.Type
This function maps one exception into another as proposed in the paper "A semantics for imprecise exceptions".
Exception context and annotation
Add more ExceptionContext to a SomeException.
View the ExceptionContext of a SomeException.
Wraps a particular exception exposing its ExceptionContext. Intended to
be used when catching exceptions in cases where access to the context is
desired.
Constructors
Instances2Show, Exception
Show a => Show (ExceptionWithContext a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Exception.TypeException a => Exception (ExceptionWithContext a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Exception.Type
WhileHandling is used to annotate rethrow exceptions. By inspecting
the WhileHandling annotation, all the places the exception has been rethrow
can be recovered.
Constructors
Instances2Show, ExceptionAnnotation
Show WhileHandlingDefined in ghc-internal-9.1401.0 · GHC.Internal.Exception.TypeExceptionAnnotation WhileHandlingDefined in ghc-internal-9.1401.0 · GHC.Internal.Exception.Type
Exception context represents a list of ExceptionAnnotations. These are
attached to SomeExceptions via addExceptionContext and
can be used to capture various ad-hoc metadata about the exception including
backtraces and application-specific context.
ExceptionContexts can be merged via concatenation using the Semigroup instance or mergeExceptionContext.
Note that GHC will automatically solve implicit constraints of type ExceptionContext with emptyExceptionContext.
Constructors
Instances2Semigroup, Monoid
Semigroup ExceptionContextDefined in ghc-internal-9.1401.0 · GHC.Internal.Exception.ContextMonoid ExceptionContextDefined in ghc-internal-9.1401.0 · GHC.Internal.Exception.Context
An ExceptionContext containing no annotations.
Construct a singleton ExceptionContext from an ExceptionAnnotation.
Retrieve all ExceptionAnnotations of the given type from an ExceptionContext.
Render ExceptionContext to a human-readable String.
Constructors
forall a. ExceptionAnnotation a => SomeExceptionAnnotation a
ExceptionAnnotations are types which can decorate exceptions as ExceptionContext.
Methods
displayExceptionAnnotation :: a -> StringRender the annotation for display to the user.
Instances2ExceptionAnnotation
ExceptionAnnotation BacktracesDefined in ghc-internal-9.1401.0 · GHC.Internal.Exception.BacktraceExceptionAnnotation WhileHandlingDefined in ghc-internal-9.1401.0 · GHC.Internal.Exception.Type
Concrete exception types
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.
Instances4Eq, Show, Exception, MonadError
Eq IOExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionShow IOExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionException IOExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionMonadError IOException IODefined in mtl-2.3.1 · Control.Monad.Error.Class
Arithmetic exceptions.
Instances4Eq, Ord, Show, Exception
Eq ArithExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.Exception.TypeOrd ArithExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.Exception.TypeShow ArithExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.Exception.TypeException ArithExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.Exception.Type
Exceptions generated by array operations
Constructors
IndexOutOfBounds StringAn attempt was made to index an array outside its declared bounds.
UndefinedElement StringAn attempt was made to evaluate an element of an array that had not been initialized.
Instances4Eq, Ord, Show, Exception
Eq ArrayExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionOrd ArrayExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionShow ArrayExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionException ArrayExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.Exception
Constructors
Instances2Show, Exception
Show AssertionFailedDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionException AssertionFailedDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.Exception
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.
Constructors
Instances2Show, Exception
Show NoMethodErrorDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Exception.BaseException NoMethodErrorDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Exception.Base
A pattern match failed. The String gives information about the
source location of the pattern.
Constructors
Instances2Show, Exception
Show PatternMatchFailDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Exception.BaseException PatternMatchFailDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Exception.Base
An uninitialised record field was used. The String gives
information about the source location where the record was
constructed.
Constructors
Instances2Show, Exception
Show RecConErrorDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Exception.BaseException RecConErrorDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Exception.Base
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.
Constructors
Instances2Show, Exception
Show RecSelErrorDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Exception.BaseException RecSelErrorDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Exception.Base
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.
Constructors
Instances2Show, Exception
Show RecUpdErrorDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Exception.BaseException RecUpdErrorDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Exception.Base
Instances4Eq, Ord, Show, Exception
Deprecated. ErrorCallWithLocation has been deprecated in favour of ErrorCall (which does not have a location). Backtraces are now handled by the backtrace exception mechanisms exclusively.
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.
Asynchronous exceptions
Superclass for asynchronous exceptions.
Constructors
forall e. Exception e => SomeAsyncException e
Instances2Show, Exception
Show SomeAsyncExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionException SomeAsyncExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.Exception
Asynchronous exceptions.
Constructors
StackOverflowThe 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.
HeapOverflowThe program's heap is reaching its limit, and the program should take action to reduce the amount of live data it has. Notes:
It is undefined which thread receives this exception. GHC currently throws this to the same thread that receives UserInterrupt, but this may change in the future.
The GHC RTS currently can only recover from heap overflow if it detects that an explicit memory limit (set via RTS flags). has been exceeded. Currently, failure to allocate memory from the operating system results in immediate termination of the program.
ThreadKilledThis exception is raised by another thread calling killThread, or by the system if it needs to terminate the thread for some reason.
UserInterruptThis 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).
Instances4Eq, Ord, Show, Exception
Eq AsyncExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionOrd AsyncExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionShow AsyncExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionException AsyncExceptionDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.Exception
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.
Instances2Show, Exception
Show NonTerminationDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Exception.BaseException NonTerminationDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Exception.Base
Thrown when the program attempts to call atomically, from the stm
package, inside another call to atomically.
Instances2Show, Exception
Show NestedAtomicallyDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Exception.BaseException NestedAtomicallyDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Exception.Base
The thread is blocked on an MVar, but there are no other references
to the MVar so it can't ever continue.
Instances2Show, Exception
Show BlockedIndefinitelyOnMVarDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionException BlockedIndefinitelyOnMVarDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.Exception
The thread is waiting to retry an STM transaction, but there are no
other references to any TVars involved, so it can't ever continue.
Instances2Show, Exception
Show BlockedIndefinitelyOnSTMDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionException BlockedIndefinitelyOnSTMDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.Exception
This thread has exceeded its allocation limit. See setAllocationCounter and enableAllocationLimit.
Instances2Show, Exception
Show AllocationLimitExceededDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionException AllocationLimitExceededDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.Exception
Compaction found an object that cannot be compacted. Functions
cannot be compacted, nor can mutable objects or pinned objects.
See compact.
Constructors
Instances2Show, Exception
Show CompactionFailedDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.ExceptionException CompactionFailedDefined in ghc-internal-9.1401.0 · GHC.Internal.IO.Exception
There are no runnable threads, so the program is deadlocked.
The Deadlock exception is raised in the main thread only.
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.