HORIZON HASKELLDocslts/ghc-10.0.xfc4e5982026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 10.0.0.20260917 · lts/ghc-10.0.x · fc4e598 · 2026-10-11

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

Throwing

2 declarations

Catching (with recovery)

26 declarations

Note: 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.

valuecatchDeep
  1. :: (Exception e, NFData a)
  2. => Eff es a
  3. -> (e -> Eff es a)

    The exception handler.

  4. -> Eff es a
#

A variant of catch that fully forces evaluation of the result value to find all impure exceptions.

valuecatchIf
  1. :: Exception e
  2. => (e -> Bool)

    The predicate.

  3. -> Eff es a
  4. -> (e -> Eff es a)

    The exception handler.

  5. -> Eff es a
#

Catch an exception only if it satisfies a specific predicate.

Cleanup (no recovery)

8 declarations

Note: 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.

valuebracket
  1. :: Eff es a

    Computation to run first.

  2. -> (a -> Eff es b)

    Computation to run last.

  3. -> (a -> Eff es c)

    Computation to run in-between.

  4. -> Eff es c
#

Lifted bracket.

valuebracket_
  1. :: Eff es a

    Computation to run first.

  2. -> Eff es b

    Computation to run last.

  3. -> Eff es c

    Computation to run in-between.

  4. -> Eff es c
#

Lifted bracket_.

Utils

0 declarations

Evaluation

Annotations

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:

Example2 expressions
data SyncEx = SyncEx deriving (Show)instance Exception SyncEx
Example1 expression
isSyncException SyncExTrue
Example1 expression
isAsyncException SyncExFalse

Whereas for asynchronous exceptions you need to define their Exception instance as follows:

Example2 expressions
data AsyncEx = AsyncEx deriving (Show):{  instance Exception AsyncEx where    toException = asyncExceptionToException    fromException = asyncExceptionFromException:}
Example1 expression
isSyncException AsyncExFalse
Example1 expression
isAsyncException AsyncExTrue

Low-level API

8 declarations
datadata MaskingState
#

Describes the behaviour of a thread when an asynchronous exception is received.

Constructors

Instances3Eq, Show, NFData

Re-exports from Control.Exception

0 declarations

The SomeException type

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

Constructors

Instances2Show, Exception

The Exception class

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

Instances32Exception, …
valuemapException :: (Exception e1, Exception e2) => (e1 -> e2) -> a -> a
#

This function maps one exception into another as proposed in the paper "A semantics for imprecise exceptions".

Exception context and annotation

datadata ExceptionContext
#

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.

Instances2Semigroup, Monoid

Concrete exception types

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

Instances4Eq, Show, Exception, MonadError
datadata ArrayException
#

Exceptions generated by array operations

Constructors

Instances4Eq, Ord, Show, Exception
newtypenewtype 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.

Constructors

Instances2Show, Exception
newtypenewtype RecConError
#

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-10.0.0 · GHC.Internal.Control.Exception.Base
  • Exception RecConErrorDefined in ghc-internal-10.0.0 · GHC.Internal.Control.Exception.Base
newtypenewtype 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.

Constructors

Instances2Show, Exception
  • Show RecSelErrorDefined in ghc-internal-10.0.0 · GHC.Internal.Control.Exception.Base
  • Exception RecSelErrorDefined in ghc-internal-10.0.0 · GHC.Internal.Control.Exception.Base
newtypenewtype 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.

Constructors

Instances2Show, Exception
  • Show RecUpdErrorDefined in ghc-internal-10.0.0 · GHC.Internal.Control.Exception.Base
  • Exception RecUpdErrorDefined in ghc-internal-10.0.0 · GHC.Internal.Control.Exception.Base
datadata ErrorCall
#

This is thrown when the user calls error. The String is the argument given to error.

Historically, there was a second String for the location, but it was subsumed by the backtrace mechanisms (since base-4.22).

Constructors

Instances4Eq, Ord, Show, Exception
  • Eq ErrorCallDefined in ghc-internal-10.0.0 · GHC.Internal.Exception
  • Ord ErrorCallDefined in ghc-internal-10.0.0 · GHC.Internal.Exception
  • Show ErrorCallDefined in ghc-internal-10.0.0 · GHC.Internal.Exception
  • Exception ErrorCallDefined in ghc-internal-10.0.0 · GHC.Internal.Exception
patternpattern ErrorCallWithLocation :: String -> String -> ErrorCall
#

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.

newtypenewtype TypeError
#

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.

Constructors

Instances2Show, Exception
  • Show TypeErrorDefined in ghc-internal-10.0.0 · GHC.Internal.Control.Exception.Base
  • Exception TypeErrorDefined in ghc-internal-10.0.0 · GHC.Internal.Control.Exception.Base

Asynchronous exceptions

datadata AsyncException
#

Asynchronous exceptions.

Constructors

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

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

  • ThreadKilled

    This exception is raised by another thread calling killThread, or by the system if it needs to terminate the thread for some reason.

  • 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).

Instances4Eq, Ord, Show, Exception
datadata 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.

Instances2Show, Exception
datadata Deadlock
#

There are no runnable threads, so the program is deadlocked. The Deadlock exception is raised in the main thread only.

Instances2Show, Exception
  • Show DeadlockDefined in ghc-internal-10.0.0 · GHC.Internal.IO.Exception
  • Exception DeadlockDefined in ghc-internal-10.0.0 · GHC.Internal.IO.Exception

Assertions

valueassert :: Bool -> a -> a
#

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.