Synchronously throw the given exception.
Note that, if you provide an exception value which is of an asynchronous
type, it will be wrapped up in SyncExceptionWrapper. See toSyncException.
:: a typeCtrl KGHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05
Moduleunliftio-0.2.25.1Haskell2010
Unlifted Control.Exception, with extra async exception safety and more helper functions.
This module works best when your cleanup functions adhere to certain expectations around exception safety and interruptible actions. For more details, see this exception safety tutorial.
Synchronously throw the given exception.
Note that, if you provide an exception value which is of an asynchronous
type, it will be wrapped up in SyncExceptionWrapper. See toSyncException.
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.
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).
Eq StringExceptionDefined in unliftio-0.2.25.1 · UnliftIO.ExceptionShow StringExceptionDefined in unliftio-0.2.25.1 · UnliftIO.ExceptionException StringExceptionDefined in unliftio-0.2.25.1 · UnliftIO.ExceptionSmart constructor for a StringException that deals with the call stack.
Throw an asynchronous exception to another thread.
Synchronously typed exceptions will be wrapped into an
AsyncExceptionWrapper, see
https://github.com/fpco/safe-exceptions#determining-sync-vs-async.
It's usually a better idea to use the UnliftIO.Async module, see https://github.com/fpco/safe-exceptions#quickstart.
Generate a pure value which, when forced, will synchronously throw the given exception.
Generally it's better to avoid using this function and instead use throwIO, see https://github.com/fpco/safe-exceptions#quickstart.
Same as fromEither, but works on an IO-wrapped Either.
Same as fromEither, but works on an m-wrapped Either.
Same as mapException, except works in a monadic context.
Catch a synchronous (but not asynchronous) exception and recover from it.
This is parameterized on the exception type. To catch all synchronous exceptions, use catchAny.
catch specialized to only catching IOExceptions.
catch specialized to catch all synchronous exceptions.
Same as catch, but fully force evaluation of the result value to find all impure exceptions.
catchDeep specialized to catch all synchronous exception.
Flipped version of catch.
handle specialized to only catching IOExceptions.
Flipped version of catchAny.
Flipped version of catchDeep.
Flipped version of catchAnyDeep.
Flipped catchJust.
try specialized to only catching IOExceptions.
try specialized to catch all synchronous exceptions.
Same as try, but fully force evaluation of the result value to find all impure exceptions.
tryDeep specialized to catch all synchronous exceptions.
A variant of try that takes an exception predicate to select which exceptions are caught.
Evaluate the value to WHNF and catch any synchronous exceptions.
The expression may still have bottom values within it; you may instead want to use pureTryDeep.
Evaluate the value to NF and catch any synchronous exceptions.
Same as catches, but fully force evaluation of the result value to find all impure exceptions.
A variant of catch that catches both synchronous and asynchronous exceptions.
WARNING: This function (and other *SyncOrAsync functions) is for advanced users. Most of the
time, you probably want to use the non-SyncOrAsync versions.
Before attempting to use this function, be familiar with the "Rules for async safe handling" section in this blog post.
A variant of handle that catches both synchronous and asynchronous exceptions.
See catchSyncOrAsync.
A variant of try that catches both synchronous and asynchronous exceptions.
See catchSyncOrAsync.
Like finally, but only call after if an exception occurs.
Allocate and clean up a resource safely.
For more information on motivation and usage of this function, see base's
bracket. This function has two differences from the one in base.
The first, and more obvious, is that it works on any MonadUnliftIO
instance, not just IO.
The more subtle difference is that this function will use uninterruptible masking for its cleanup handler. This is a subtle distinction, but at a high level, means that resource cleanup has more guarantees to complete. This comes at the cost that an incorrectly written cleanup function cannot be interrupted.
For more information, please see https://github.com/fpco/safe-exceptions/issues/3.
Like onException, but provides the handler the thrown exception.
Same as bracket, but only perform the cleanup if an exception is thrown.
A variant of bracketOnError where the return value from the first computation is not required.
In version 0.2.23.0, these were changed with aliases to the values
from Control.Exception.Safe in the safe-exceptions package.
Wrap up an asynchronous exception to be treated as a synchronous exception
This is intended to be created via toSyncException
forall e. Exception e => SyncExceptionWrapper eShow SyncExceptionWrapperDefined in safe-exceptions-0.1.7.4 · Control.Exception.SafeException SyncExceptionWrapperDefined in safe-exceptions-0.1.7.4 · Control.Exception.SafeConvert 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.
Wrap up a synchronous exception to be treated as an asynchronous exception
This is intended to be created via toAsyncException
forall e. Exception e => AsyncExceptionWrapper eShow AsyncExceptionWrapperDefined in safe-exceptions-0.1.7.4 · Control.Exception.SafeException AsyncExceptionWrapperDefined in safe-exceptions-0.1.7.4 · Control.Exception.SafeConvert 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.
Convert from a possibly wrapped exception.
The inverse of toAsyncException and toSyncException. When using those
functions (or functions that use them, like throwTo or throwIO),
fromException might not be sufficient because the exception might be
wrapped within SyncExceptionWrapper or AsyncExceptionWrapper.
Check if the given exception is synchronous.
Check if the given exception is asynchronous.
Unlifted version of mask.
Unlifted version of uninterruptibleMask.
Unlifted version of mask_.
Unlifted version of uninterruptibleMask_.
Lifted version of evaluate.
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
toException :: e -> SomeExceptiontoException should 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 -> BoolException AsyncCancelledDefined in async-2.2.5 · Control.Concurrent.Async.InternalException ExceptionInLinkedThreadDefined in async-2.2.5 · Control.Concurrent.Async.InternalException TimeoutDefined in base-4.20.2.0 · System.TimeoutException SizeOverflowExceptionDefined in bytestring-0.12.2.0 · Data.ByteString.Internal.TypeException VoidDefined in ghc-internal-9.1003.0 · GHC.Internal.Exception.TypeException NestedAtomicallyDefined in ghc-internal-9.1003.0 · GHC.Internal.Control.Exception.BaseException NoMatchingContinuationPromptDefined in ghc-internal-9.1003.0 · GHC.Internal.Control.Exception.BaseException NoMethodErrorDefined in ghc-internal-9.1003.0 · GHC.Internal.Control.Exception.BaseException NonTerminationDefined in ghc-internal-9.1003.0 · GHC.Internal.Control.Exception.BaseException PatternMatchFailDefined in ghc-internal-9.1003.0 · GHC.Internal.Control.Exception.BaseException RecConErrorDefined in ghc-internal-9.1003.0 · GHC.Internal.Control.Exception.BaseException RecSelErrorDefined in ghc-internal-9.1003.0 · GHC.Internal.Control.Exception.BaseException RecUpdErrorDefined in ghc-internal-9.1003.0 · GHC.Internal.Control.Exception.BaseException TypeErrorDefined in ghc-internal-9.1003.0 · GHC.Internal.Control.Exception.BaseException DynamicDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.DynamicException ErrorCallDefined in ghc-internal-9.1003.0 · GHC.Internal.ExceptionException ArithExceptionDefined in ghc-internal-9.1003.0 · GHC.Internal.Exception.TypeException SomeExceptionDefined in ghc-internal-9.1003.0 · GHC.Internal.Exception.TypeThis drops any attached ExceptionContext.
Exception AllocationLimitExceededDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.ExceptionException ArrayExceptionDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.ExceptionException AssertionFailedDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.ExceptionException AsyncExceptionDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.ExceptionException BlockedIndefinitelyOnMVarDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.ExceptionException BlockedIndefinitelyOnSTMDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.ExceptionException CompactionFailedDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.ExceptionException DeadlockDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.ExceptionException ExitCodeDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.ExceptionException FixIOExceptionDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.ExceptionException IOExceptionDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.ExceptionException SomeAsyncExceptionDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.ExceptionException FileLockingNotSupportedDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.Handle.Lock.CommonException IOPortExceptionDefined in ghc-internal-9.1003.0 · GHC.Internal.IOPortException EncodingExceptionDefined in os-string-2.0.7 · System.OsString.Encoding.InternalException AsyncExceptionWrapperDefined in safe-exceptions-0.1.7.4 · Control.Exception.SafeException StringExceptionDefined in safe-exceptions-0.1.7.4 · Control.Exception.SafeException SyncExceptionWrapperDefined in safe-exceptions-0.1.7.4 · Control.Exception.SafeException UnicodeExceptionDefined in text-2.1.3 · Data.Text.Encoding.ErrorException StringExceptionDefined in unliftio-0.2.25.1 · UnliftIO.ExceptionException ConcExceptionDefined in unliftio-0.2.25.1 · UnliftIO.Internals.AsyncException a => Exception (ExceptionWithContext a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Exception.TypeException e => Exception (NoBacktrace e)Defined in ghc-internal-9.1003.0 · GHC.Internal.Exception.TypeThe class Typeable allows a concrete representation of a type to be calculated.
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.
forall e. (Exception e, HasExceptionContext) => SomeException eShow SomeExceptionDefined in ghc-internal-9.1003.0 · GHC.Internal.Exception.TypeException SomeExceptionDefined in ghc-internal-9.1003.0 · GHC.Internal.Exception.TypeThis drops any attached ExceptionContext.
Superclass for asynchronous exceptions.
forall e. Exception e => SomeAsyncException eShow SomeAsyncExceptionDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.ExceptionException SomeAsyncExceptionDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.ExceptionExceptions 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.
Eq IOExceptionDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.ExceptionShow IOExceptionDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.ExceptionException IOExceptionDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.ExceptionMonadError IOException IODefined in mtl-2.3.1 · Control.Monad.Error.ClassIf 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.