primal-0.3.0.0: Primeval world of Haskell.
Copyright(c) Alexey Kuleshevich 2020
LicenseBSD3
MaintainerAlexey Kuleshevich <alexey@kuleshevi.ch>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Control.Prim.Exception

Description

 
Synopsis

Throwing

throw :: (Exception e, MonadPrim s m) => e -> m a Source #

This is the same as throwIO, but works with any MonadPrim without restriction on RealWorld.

throwTo :: (MonadPrim s m, Exception e) => ThreadId -> e -> m () Source #

Similar to throwTo, except that it wraps any known non-async exception with SomeAsyncException. This is necessary, because receiving thread will get the exception in an asynchronous manner and without proper wrapping it will not be able to distinguish it from a regular synchronous exception

impureThrow :: Exception e => e -> a Source #

Raise an impure exception from pure code. Returns a thunk, which will result in a supplied exceptionn being thrown when evaluated.

Since: 0.3.0

Catching

catch :: forall e a m. (Exception e, MonadUnliftPrim RW m) => m a -> (e -> m a) -> m a Source #

Behaves exactly as catch, except that it works in any MonadUnliftPrim.

catchAny :: forall a m. MonadUnliftPrim RW m => m a -> (SomeException -> m a) -> m a Source #

catchAnySync :: forall a m. MonadUnliftPrim RW m => m a -> (SomeException -> m a) -> m a Source #

catchAll :: forall a m. MonadUnliftPrim RW m => m a -> (forall e. Exception e => e -> m a) -> m a Source #

catchAllSync :: forall a m. MonadUnliftPrim RW m => m a -> (forall e. Exception e => e -> m a) -> m a Source #

try :: (Exception e, MonadUnliftPrim RW m) => m a -> m (Either e a) Source #

onException :: MonadUnliftPrim RW m => m a -> m b -> m a Source #

Async safe version of onException.

Since: 0.1.0.0

withException :: (MonadUnliftPrim RW m, Exception e) => m a -> (e -> m b) -> m a Source #

Run an action, while invoking an exception handler if that action fails for some reason. Exception handling function has async exceptions masked, but it is still interruptible, which can be undesired in some scenarios. If you are sure that the cleanup action does not deadlock and you do need hard guarantees that it gets executed you can run it as uninterruptible:

uninterruptibleMask $ \restore -> withException (restore action) handler

Since: 0.3.0

withAnyException :: MonadUnliftPrim RW m => m a -> (SomeException -> m b) -> m a Source #

Same as withException, but will invoke exception handling function on all exceptions.

Since: 0.3.0

finally :: MonadUnliftPrim RW m => m a -> m b -> m a Source #

bracket :: MonadUnliftPrim RW m => m a -> (a -> m b) -> (a -> m c) -> m c Source #

bracket_ :: MonadUnliftPrim RW m => m a -> m b -> m c -> m c Source #

bracketOnError :: MonadUnliftPrim RW m => m a -> (a -> m b) -> (a -> m c) -> m c Source #

ufinally :: MonadUnliftPrim RW m => m a -> m b -> m a Source #

ubracket :: MonadUnliftPrim RW m => m a -> (a -> m b) -> (a -> m c) -> m c Source #

ubracket_ :: MonadUnliftPrim RW m => m a -> m b -> m c -> m c Source #

ubracketOnError :: MonadUnliftPrim RW m => m a -> (a -> m b) -> (a -> m c) -> m c Source #

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

Mask all asychronous exceptions, but keep it interruptible, unless the inherited state was uninterruptible already, in which case this action has no affect. Same as mask, except that it is polymorphic in state token. Inside a state thread it cannot affect the result of computation, therefore it is safe to use it within ST monad.

Since: 0.3.0

mask_ :: forall a m s. MonadUnliftPrim s m => m a -> m a Source #

Mask all asychronous exceptions, but keep it interruptible, unless the inherited state was uninterruptible already, in which case this action has no affect. Same as mask_, except that it is polymorphic in state token. Inside a state thread it cannot affect the result of computation, therefore it is safe to use it within ST monad.

Since: 0.3.0

maskPrimBase_ :: forall a n m s. (MonadPrim s m, MonadPrimBase s n) => n a -> m a Source #

uninterruptibleMask :: forall a m s. MonadUnliftPrim s m => ((forall b. m b -> m b) -> m a) -> m a Source #

Mask all asychronous exceptions and mark it uninterruptible. Same as uninterruptibleMask, except that it is polymorphic in state token. Inside a state thread it cannot affect the result of computation, therefore it is safe to use it within ST monad.

Since: 0.3.0

uninterruptibleMask_ :: forall a m s. MonadUnliftPrim s m => m a -> m a Source #

Mask all async exceptions and make sure evaluation cannot be interrupted. It is polymorphic in the state token because it is perfectly safe to use with ST actions that don't perform any allocations. It doesn't have to be restricted to RealWorld because it has no impact on other threads and can't affect the result of computation, moreover pure functions that implement tight loops are already non-interruptible. In fact using this function is more dangerous in IO than it is in ST, because misuse can lead to deadlocks in a concurrent setting.

Since: 0.3.0

uninterruptibleMaskPrimBase_ :: forall a n m s. (MonadPrimBase s n, MonadPrim s m) => n a -> m a Source #

maskAsyncExceptions :: forall a m. MonadUnliftPrim RW m => m a -> m a Source #

A direct wrapper around maskAsyncExceptions# primop. This is different and more dangerous than mask_ because it can turn uninterrubtable state into interruptable.

unmaskAsyncExceptions :: forall a m. MonadUnliftPrim RW m => m a -> m a Source #

A direct wrapper around unmaskAsyncExceptions# primop.

maskUninterruptible :: forall a m. MonadUnliftPrim RW m => m a -> m a Source #

A direct wrapper around maskUninterruptible# primop.

data MaskingState #

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

Constructors

Unmasked

asynchronous exceptions are unmasked (the normal state)

MaskedInterruptible

the state during mask: asynchronous exceptions are masked, but blocking operations may still be interrupted

MaskedUninterruptible

the state during uninterruptibleMask: asynchronous exceptions are masked, and blocking operations may not be interrupted

Instances

Instances details
Eq MaskingState

Since: base-4.3.0.0

Instance details

Defined in GHC.IO

Show MaskingState

Since: base-4.3.0.0

Instance details

Defined in GHC.IO

NFData MaskingState

Since: deepseq-1.4.4.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: MaskingState -> () #

getMaskingState :: MonadPrim RW m => m MaskingState Source #

Same as getMaskingState, but generalized to MonadPrim

Since: 0.3.0

Exceptions

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

Minimal complete definition

Nothing

Methods

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

Instances details
Exception Void

Since: base-4.8.0.0

Instance details

Defined in Data.Void

Exception Timeout

Since: base-4.7.0.0

Instance details

Defined in System.Timeout

Exception PatternMatchFail

Since: base-4.0

Instance details

Defined in Control.Exception.Base

Exception RecSelError

Since: base-4.0

Instance details

Defined in Control.Exception.Base

Exception RecConError

Since: base-4.0

Instance details

Defined in Control.Exception.Base

Exception RecUpdError

Since: base-4.0

Instance details

Defined in Control.Exception.Base

Exception NoMethodError

Since: base-4.0

Instance details

Defined in Control.Exception.Base

Exception TypeError

Since: base-4.9.0.0

Instance details

Defined in Control.Exception.Base

Exception NonTermination

Since: base-4.0

Instance details

Defined in Control.Exception.Base

Exception NestedAtomically

Since: base-4.0

Instance details

Defined in Control.Exception.Base

Exception BlockedIndefinitelyOnMVar

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception BlockedIndefinitelyOnSTM

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception Deadlock

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception AllocationLimitExceeded

Since: base-4.8.0.0

Instance details

Defined in GHC.IO.Exception

Exception CompactionFailed

Since: base-4.10.0.0

Instance details

Defined in GHC.IO.Exception

Exception AssertionFailed

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception SomeAsyncException

Since: base-4.7.0.0

Instance details

Defined in GHC.IO.Exception

Exception AsyncException

Since: base-4.7.0.0

Instance details

Defined in GHC.IO.Exception

Exception ArrayException

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception FixIOException

Since: base-4.11.0.0

Instance details

Defined in GHC.IO.Exception

Exception ExitCode

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception IOException

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception ErrorCall

Since: base-4.0.0.0

Instance details

Defined in GHC.Exception

Exception ArithException

Since: base-4.0.0.0

Instance details

Defined in GHC.Exception.Type

Exception SomeException

Since: base-3.0

Instance details

Defined in GHC.Exception.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.

Instances

Instances details
Show SomeException

Since: base-3.0

Instance details

Defined in GHC.Exception.Type

Exception SomeException

Since: base-3.0

Instance details

Defined in GHC.Exception.Type

Async exceptions

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

data SomeAsyncException #

Superclass for asynchronous exceptions.

Since: base-4.7.0.0

Constructors

Exception e => SomeAsyncException e 

asyncExceptionToException :: Exception e => e -> SomeException #

Since: base-4.7.0.0

Standard exceptions

data ErrorCall #

This is thrown when the user calls error. The first String is the argument given to error, second String is the location.

Bundled Patterns

pattern ErrorCall :: String -> ErrorCall 

Instances

Instances details
Eq ErrorCall

Since: base-4.7.0.0

Instance details

Defined in GHC.Exception

Ord ErrorCall

Since: base-4.7.0.0

Instance details

Defined in GHC.Exception

Show ErrorCall

Since: base-4.0.0.0

Instance details

Defined in GHC.Exception

Exception ErrorCall

Since: base-4.0.0.0

Instance details

Defined in GHC.Exception

data ArrayException #

Exceptions generated by array operations

Constructors

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.

newtype AssertionFailed #

assert was applied to False.

Constructors

AssertionFailed String 

Instances

Instances details
Show AssertionFailed

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception AssertionFailed

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

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

Instances details
Eq IOException

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Show IOException

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception IOException

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

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.

Constructors

NonTermination 

data NestedAtomically #

Thrown when the program attempts to call atomically, from the stm package, inside another call to atomically.

Constructors

NestedAtomically 

data BlockedIndefinitelyOnMVar #

The thread is blocked on an MVar, but there are no other references to the MVar so it can't ever continue.

data BlockedIndefinitelyOnSTM #

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.

data AllocationLimitExceeded #

This thread has exceeded its allocation limit. See setAllocationCounter and enableAllocationLimit.

Since: base-4.8.0.0

data Deadlock #

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

Constructors

Deadlock 

Instances

Instances details
Show Deadlock

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception Deadlock

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

CallStack

data CallStack #

CallStacks are a lightweight method of obtaining a partial call-stack at any point in the program.

A function can request its call-site with the HasCallStack constraint. For example, we can define

putStrLnWithCallStack :: HasCallStack => String -> IO ()

as a variant of putStrLn that will get its call-site and print it, along with the string given as argument. We can access the call-stack inside putStrLnWithCallStack with callStack.

putStrLnWithCallStack :: HasCallStack => String -> IO ()
putStrLnWithCallStack msg = do
  putStrLn msg
  putStrLn (prettyCallStack callStack)

Thus, if we call putStrLnWithCallStack we will get a formatted call-stack alongside our string.

>>> putStrLnWithCallStack "hello"
hello
CallStack (from HasCallStack):
  putStrLnWithCallStack, called at <interactive>:2:1 in interactive:Ghci1

GHC solves HasCallStack constraints in three steps:

  1. If there is a CallStack in scope -- i.e. the enclosing function has a HasCallStack constraint -- GHC will append the new call-site to the existing CallStack.
  2. If there is no CallStack in scope -- e.g. in the GHCi session above -- and the enclosing definition does not have an explicit type signature, GHC will infer a HasCallStack constraint for the enclosing definition (subject to the monomorphism restriction).
  3. If there is no CallStack in scope and the enclosing definition has an explicit type signature, GHC will solve the HasCallStack constraint for the singleton CallStack containing just the current call-site.

CallStacks do not interact with the RTS and do not require compilation with -prof. On the other hand, as they are built up explicitly via the HasCallStack constraints, they will generally not contain as much information as the simulated call-stacks maintained by the RTS.

A CallStack is a [(String, SrcLoc)]. The String is the name of function that was called, the SrcLoc is the call-site. The list is ordered with the most recently called function at the head.

NOTE: The intrepid user may notice that HasCallStack is just an alias for an implicit parameter ?callStack :: CallStack. This is an implementation detail and should not be considered part of the CallStack API, we may decide to change the implementation in the future.

Since: base-4.8.1.0

Instances

Instances details
IsList CallStack

Be aware that 'fromList . toList = id' only for unfrozen CallStacks, since toList removes frozenness information.

Since: base-4.9.0.0

Instance details

Defined in GHC.Exts

Associated Types

type Item CallStack #

Show CallStack

Since: base-4.9.0.0

Instance details

Defined in GHC.Show

NFData CallStack

Since: deepseq-1.4.2.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CallStack -> () #

type Item CallStack 
Instance details

Defined in GHC.Exts

type HasCallStack = ?callStack :: CallStack #

Request a CallStack.

NOTE: The implicit parameter ?callStack :: CallStack is an implementation detail and should not be considered part of the CallStack API, we may decide to change the implementation in the future.

Since: base-4.9.0.0

callStack :: HasCallStack => CallStack #

Return the current CallStack.

Does *not* include the call-site of callStack.

Since: base-4.9.0.0

getCallStack :: CallStack -> [([Char], SrcLoc)] #

Extract a list of call-sites from the CallStack.

The list is ordered by most recent call.

Since: base-4.8.1.0

prettyCallStack :: CallStack -> String #

Pretty print a CallStack.

Since: base-4.9.0.0

data SrcLoc #

A single location in the source code.

Since: base-4.8.1.0

Instances

Instances details
Eq SrcLoc

Since: base-4.9.0.0

Instance details

Defined in GHC.Stack.Types

Methods

(==) :: SrcLoc -> SrcLoc -> Bool #

(/=) :: SrcLoc -> SrcLoc -> Bool #

Show SrcLoc

Since: base-4.9.0.0

Instance details

Defined in GHC.Show

NFData SrcLoc

Since: deepseq-1.4.2.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: SrcLoc -> () #

prettySrcLoc :: SrcLoc -> String #

Pretty print a SrcLoc.

Since: base-4.9.0.0