extra
Safe HaskellSafe-Inferred
LanguageHaskell2010

Control.Exception.Extra

Description

Extra functions for Control.Exception. These functions provide retrying, showing in the presence of exceptions, and functions to catch/ignore exceptions, including monomorphic (no Exception context) versions.

If you want to use a safer set of exceptions see the safe-exceptions package.

Synopsis

Documentation

assert :: Bool -> a -> a #

throwTo :: Exception e => ThreadId -> e -> IO () #

catches :: IO a -> [Handler a] -> IO a #

bracket :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c #

bracketOnError :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c #

bracket_ :: IO a -> IO b -> IO c -> IO c #

catchJust :: Exception e => (e -> Maybe b) -> IO a -> (b -> IO a) -> IO a #

finally :: IO a -> IO b -> IO a #

handle :: Exception e => (e -> IO a) -> IO a -> IO a #

handleJust :: Exception e => (e -> Maybe b) -> (b -> IO a) -> IO a -> IO a #

mapException :: (Exception e1, Exception e2) => (e1 -> e2) -> a -> a #

onException :: IO a -> IO b -> IO a #

try :: Exception e => IO a -> IO (Either e a) #

tryJust :: Exception e => (e -> Maybe b) -> IO a -> IO (Either b a) #

throw :: forall a e. (HasCallStack, Exception e) => e -> a #

addExceptionContext :: ExceptionAnnotation a => a -> SomeException -> SomeException #

someExceptionContext :: SomeException -> ExceptionContext #

annotateIO :: ExceptionAnnotation e => e -> IO a -> IO a #

catch :: Exception e => IO a -> (e -> IO a) -> IO a #

evaluate :: a -> IO a #

interruptible :: IO a -> IO a #

mask :: ((forall a. IO a -> IO a) -> IO b) -> IO b #

mask_ :: IO a -> IO a #

throwIO :: (HasCallStack, Exception e) => e -> IO a #

uninterruptibleMask :: ((forall a. IO a -> IO a) -> IO b) -> IO b #

ioError :: HasCallStack => IOError -> IO a #

data Handler a #

Constructors

Exception e => Handler (e -> IO a) 

Instances

Instances details
Functor Handler # 
Instance details

Defined in GHC.Internal.Control.Exception

Methods

fmap :: (a -> b) -> Handler a -> Handler b #

(<$) :: a -> Handler b -> Handler a #

newtype NoMethodError #

Constructors

NoMethodError String 

Instances

Instances details
Exception NoMethodError # 
Instance details

Defined in GHC.Internal.Control.Exception.Base

Show NoMethodError # 
Instance details

Defined in GHC.Internal.Control.Exception.Base

Methods

showsPrec :: Int -> NoMethodError -> ShowS

show :: NoMethodError -> String

showList :: [NoMethodError] -> ShowS

data NonTermination #

Constructors

NonTermination 

Instances

Instances details
Exception NonTermination # 
Instance details

Defined in GHC.Internal.Control.Exception.Base

Show NonTermination # 
Instance details

Defined in GHC.Internal.Control.Exception.Base

Methods

showsPrec :: Int -> NonTermination -> ShowS

show :: NonTermination -> String

showList :: [NonTermination] -> ShowS

newtype RecConError #

Constructors

RecConError String 

Instances

Instances details
Exception RecConError # 
Instance details

Defined in GHC.Internal.Control.Exception.Base

Show RecConError # 
Instance details

Defined in GHC.Internal.Control.Exception.Base

Methods

showsPrec :: Int -> RecConError -> ShowS

show :: RecConError -> String

showList :: [RecConError] -> ShowS

newtype RecSelError #

Constructors

RecSelError String 

Instances

Instances details
Exception RecSelError # 
Instance details

Defined in GHC.Internal.Control.Exception.Base

Show RecSelError # 
Instance details

Defined in GHC.Internal.Control.Exception.Base

Methods

showsPrec :: Int -> RecSelError -> ShowS

show :: RecSelError -> String

showList :: [RecSelError] -> ShowS

newtype RecUpdError #

Constructors

RecUpdError String 

Instances

Instances details
Exception RecUpdError # 
Instance details

Defined in GHC.Internal.Control.Exception.Base

Show RecUpdError # 
Instance details

Defined in GHC.Internal.Control.Exception.Base

Methods

showsPrec :: Int -> RecUpdError -> ShowS

show :: RecUpdError -> String

showList :: [RecUpdError] -> ShowS

newtype TypeError #

Constructors

TypeError String 

Instances

Instances details
Exception TypeError # 
Instance details

Defined in GHC.Internal.Control.Exception.Base

Show TypeError # 
Instance details

Defined in GHC.Internal.Control.Exception.Base

Methods

showsPrec :: Int -> TypeError -> ShowS

show :: TypeError -> String

showList :: [TypeError] -> ShowS

data ErrorCall #

Constructors

ErrorCall String 

Bundled Patterns

pattern ErrorCallWithLocation :: String -> String -> ErrorCall 

Instances

Instances details
Exception ErrorCall # 
Instance details

Defined in GHC.Internal.Exception

Show ErrorCall # 
Instance details

Defined in GHC.Internal.Exception

Methods

showsPrec :: Int -> ErrorCall -> ShowS

show :: ErrorCall -> String

showList :: [ErrorCall] -> ShowS

Eq ErrorCall # 
Instance details

Defined in GHC.Internal.Exception

Methods

(==) :: ErrorCall -> ErrorCall -> Bool

(/=) :: ErrorCall -> ErrorCall -> Bool

Ord ErrorCall # 
Instance details

Defined in GHC.Internal.Exception

Methods

compare :: ErrorCall -> ErrorCall -> Ordering

(<) :: ErrorCall -> ErrorCall -> Bool

(<=) :: ErrorCall -> ErrorCall -> Bool

(>) :: ErrorCall -> ErrorCall -> Bool

(>=) :: ErrorCall -> ErrorCall -> Bool

max :: ErrorCall -> ErrorCall -> ErrorCall

min :: ErrorCall -> ErrorCall -> ErrorCall

class (Typeable e, Show e) => Exception e where #

Minimal complete definition

Nothing

Methods

toException :: e -> SomeException #

fromException :: SomeException -> Maybe e #

displayException :: e -> String #

backtraceDesired :: e -> Bool #

Instances

Instances details
Exception Timeout # 
Instance details

Defined in System.Timeout

Methods

toException :: Timeout -> SomeException #

fromException :: SomeException -> Maybe Timeout #

displayException :: Timeout -> String #

backtraceDesired :: Timeout -> Bool #

Exception SizeOverflowException # 
Instance details

Defined in Data.ByteString.Internal.Type

Methods

toException :: SizeOverflowException -> SomeException #

fromException :: SomeException -> Maybe SizeOverflowException #

displayException :: SizeOverflowException -> String #

backtraceDesired :: SizeOverflowException -> Bool #

Exception Void # 
Instance details

Defined in GHC.Internal.Exception.Type

Methods

toException :: Void -> SomeException #

fromException :: SomeException -> Maybe Void #

displayException :: Void -> String #

backtraceDesired :: Void -> Bool #

Exception NestedAtomically # 
Instance details

Defined in GHC.Internal.Control.Exception.Base

Exception NoMatchingContinuationPrompt # 
Instance details

Defined in GHC.Internal.Control.Exception.Base

Methods

toException :: NoMatchingContinuationPrompt -> SomeException #

fromException :: SomeException -> Maybe NoMatchingContinuationPrompt #

displayException :: NoMatchingContinuationPrompt -> String #

backtraceDesired :: NoMatchingContinuationPrompt -> Bool #

Exception NoMethodError # 
Instance details

Defined in GHC.Internal.Control.Exception.Base

Exception NonTermination # 
Instance details

Defined in GHC.Internal.Control.Exception.Base

Exception PatternMatchFail # 
Instance details

Defined in GHC.Internal.Control.Exception.Base

Exception RecConError # 
Instance details

Defined in GHC.Internal.Control.Exception.Base

Exception RecSelError # 
Instance details

Defined in GHC.Internal.Control.Exception.Base

Exception RecUpdError # 
Instance details

Defined in GHC.Internal.Control.Exception.Base

Exception TypeError # 
Instance details

Defined in GHC.Internal.Control.Exception.Base

Exception ErrorCall # 
Instance details

Defined in GHC.Internal.Exception

Exception ArithException # 
Instance details

Defined in GHC.Internal.Exception.Type

Exception SomeException # 
Instance details

Defined in GHC.Internal.Exception.Type

Exception AllocationLimitExceeded # 
Instance details

Defined in GHC.Internal.IO.Exception

Exception ArrayException # 
Instance details

Defined in GHC.Internal.IO.Exception

Exception AssertionFailed # 
Instance details

Defined in GHC.Internal.IO.Exception

Exception AsyncException # 
Instance details

Defined in GHC.Internal.IO.Exception

Exception BlockedIndefinitelyOnMVar # 
Instance details

Defined in GHC.Internal.IO.Exception

Exception BlockedIndefinitelyOnSTM # 
Instance details

Defined in GHC.Internal.IO.Exception

Exception CompactionFailed # 
Instance details

Defined in GHC.Internal.IO.Exception

Exception Deadlock # 
Instance details

Defined in GHC.Internal.IO.Exception

Exception ExitCode # 
Instance details

Defined in GHC.Internal.IO.Exception

Methods

toException :: ExitCode -> SomeException #

fromException :: SomeException -> Maybe ExitCode #

displayException :: ExitCode -> String #

backtraceDesired :: ExitCode -> Bool #

Exception FixIOException # 
Instance details

Defined in GHC.Internal.IO.Exception

Methods

toException :: FixIOException -> SomeException #

fromException :: SomeException -> Maybe FixIOException #

displayException :: FixIOException -> String #

backtraceDesired :: FixIOException -> Bool #

Exception IOException # 
Instance details

Defined in GHC.Internal.IO.Exception

Exception SomeAsyncException # 
Instance details

Defined in GHC.Internal.IO.Exception

Exception a => Exception (ExceptionWithContext a) # 
Instance details

Defined in GHC.Internal.Exception.Type

Exception e => Exception (NoBacktrace e) # 
Instance details

Defined in GHC.Internal.Exception.Type

data ExceptionWithContext a #

Constructors

ExceptionWithContext ExceptionContext a 

Instances

Instances details
Exception a => Exception (ExceptionWithContext a) # 
Instance details

Defined in GHC.Internal.Exception.Type

Show a => Show (ExceptionWithContext a) # 
Instance details

Defined in GHC.Internal.Exception.Type

Methods

showsPrec :: Int -> ExceptionWithContext a -> ShowS

show :: ExceptionWithContext a -> String

showList :: [ExceptionWithContext a] -> ShowS

newtype NoBacktrace e #

Constructors

NoBacktrace e 

Instances

Instances details
Exception e => Exception (NoBacktrace e) # 
Instance details

Defined in GHC.Internal.Exception.Type

Show e => Show (NoBacktrace e) # 
Instance details

Defined in GHC.Internal.Exception.Type

Methods

showsPrec :: Int -> NoBacktrace e -> ShowS

show :: NoBacktrace e -> String

showList :: [NoBacktrace e] -> ShowS

data SomeException #

Constructors

(Exception e, HasExceptionContext) => SomeException e 

Instances

Instances details
Exception SomeException # 
Instance details

Defined in GHC.Internal.Exception.Type

Show SomeException # 
Instance details

Defined in GHC.Internal.Exception.Type

Methods

showsPrec :: Int -> SomeException -> ShowS

show :: SomeException -> String

showList :: [SomeException] -> ShowS

data WhileHandling #

Instances

Instances details
ExceptionAnnotation WhileHandling # 
Instance details

Defined in GHC.Internal.Exception.Type

Show WhileHandling # 
Instance details

Defined in GHC.Internal.Exception.Type

Methods

showsPrec :: Int -> WhileHandling -> ShowS

show :: WhileHandling -> String

showList :: [WhileHandling] -> ShowS

data MaskingState #

Instances

Instances details
NFData MaskingState # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: MaskingState -> ()

Show MaskingState # 
Instance details

Defined in GHC.Internal.IO

Methods

showsPrec :: Int -> MaskingState -> ShowS

show :: MaskingState -> String

showList :: [MaskingState] -> ShowS

Eq MaskingState # 
Instance details

Defined in GHC.Internal.IO

Methods

(==) :: MaskingState -> MaskingState -> Bool

(/=) :: MaskingState -> MaskingState -> Bool

newtype AssertionFailed #

Constructors

AssertionFailed String 

Instances

Instances details
Exception AssertionFailed # 
Instance details

Defined in GHC.Internal.IO.Exception

Show AssertionFailed # 
Instance details

Defined in GHC.Internal.IO.Exception

Methods

showsPrec :: Int -> AssertionFailed -> ShowS

show :: AssertionFailed -> String

showList :: [AssertionFailed] -> ShowS

newtype CompactionFailed #

Constructors

CompactionFailed String 

Instances

Instances details
Exception CompactionFailed # 
Instance details

Defined in GHC.Internal.IO.Exception

Show CompactionFailed # 
Instance details

Defined in GHC.Internal.IO.Exception

Methods

showsPrec :: Int -> CompactionFailed -> ShowS

show :: CompactionFailed -> String

showList :: [CompactionFailed] -> ShowS

data Deadlock #

Constructors

Deadlock 

Instances

Instances details
Exception Deadlock # 
Instance details

Defined in GHC.Internal.IO.Exception

Show Deadlock # 
Instance details

Defined in GHC.Internal.IO.Exception

Methods

showsPrec :: Int -> Deadlock -> ShowS

show :: Deadlock -> String

showList :: [Deadlock] -> ShowS

data IOException #

Instances

Instances details
Exception IOException # 
Instance details

Defined in GHC.Internal.IO.Exception

Show IOException # 
Instance details

Defined in GHC.Internal.IO.Exception

Methods

showsPrec :: Int -> IOException -> ShowS

show :: IOException -> String

showList :: [IOException] -> ShowS

Eq IOException # 
Instance details

Defined in GHC.Internal.IO.Exception

Methods

(==) :: IOException -> IOException -> Bool

(/=) :: IOException -> IOException -> Bool

type Partial = HasCallStack Source #

A constraint which documents that a function is partial, and on GHC 8.0 and above produces a stack trace on failure. For example:

myHead :: Partial => [a] -> a
myHead [] = error "bad"
myHead (x:xs) = x

When using Partial with GHC 7.8 or below you need to enable the language feature ConstraintKinds, e.g. {-# LANGUAGE ConstraintKinds #-} at the top of the file.

retry :: Int -> IO a -> IO a Source #

Retry an operation at most n times (n must be positive). If the operation fails the nth time it will throw that final exception.

retry 1 (print "x")  == print "x"
retry 3 (fail "die") == fail "die"

retryBool :: Exception e => (e -> Bool) -> Int -> IO a -> IO a Source #

Retry an operation at most n times (n must be positive), while the exception value and type match a predicate. If the operation fails the nth time it will throw that final exception.

errorWithoutStackTrace :: [Char] -> a #

showException :: Show e => e -> IO String Source #

Show a value, but if the result contains exceptions, produce <Exception>. Defined as stringException . show. Particularly useful for printing exceptions to users, remembering that exceptions can themselves contain undefined values.

stringException :: String -> IO String Source #

Fully evaluate an input String. If the String contains embedded exceptions it will produce <Exception>.

stringException "test"                           == pure "test"
stringException ("test" ++ undefined)            == pure "test<Exception>"
stringException ("test" ++ undefined ++ "hello") == pure "test<Exception>"
stringException ['t','e','s','t',undefined]      == pure "test<Exception>"

errorIO :: Partial => String -> IO a Source #

An IO action that when evaluated calls error, in the IO monad. Note that while fail in IO raises an IOException, this function raises an ErrorCall exception with a call stack.

catch (errorIO "Hello") (\(ErrorCall x) -> pure x) == pure "Hello"
seq (errorIO "foo") (print 1) == print 1

assertIO :: Partial => Bool -> IO () Source #

An IO action that when evaluated calls assert in the IO monad, which throws an AssertionFailed exception if the argument is False. With optimizations enabled (and -fgnore-asserts) this function ignores its argument and does nothing.

catch (assertIO True  >> pure 1) (\(x :: AssertionFailed) -> pure 2) == pure 1
seq (assertIO False) (print 1) == print 1

Exception catching/ignoring

ignore :: IO () -> IO () Source #

Ignore any exceptions thrown by the action.

ignore (print 1)    == print 1
ignore (fail "die") == pure ()

catch_ :: IO a -> (SomeException -> IO a) -> IO a Source #

A version of catch without the Exception context, restricted to SomeException, so catches all exceptions.

handle_ :: (SomeException -> IO a) -> IO a -> IO a Source #

Like catch_ but for handle

try_ :: IO a -> IO (Either SomeException a) Source #

Like catch_ but for try

catchJust_ :: (SomeException -> Maybe b) -> IO a -> (b -> IO a) -> IO a Source #

Like catch_ but for catchJust

handleJust_ :: (SomeException -> Maybe b) -> (b -> IO a) -> IO a -> IO a Source #

Like catch_ but for handleJust

tryJust_ :: (SomeException -> Maybe b) -> IO a -> IO (Either b a) Source #

Like catch_ but for tryJust

catchBool :: Exception e => (e -> Bool) -> IO a -> (e -> IO a) -> IO a Source #

Catch an exception if the predicate passes, then call the handler with the original exception. As an example:

readFileExists x == catchBool isDoesNotExistError (readFile "myfile") (const $ pure "")

handleBool :: Exception e => (e -> Bool) -> (e -> IO a) -> IO a -> IO a Source #

Like catchBool but for handle.

tryBool :: Exception e => (e -> Bool) -> IO a -> IO (Either e a) Source #

Like catchBool but for try.