{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE NoImplicitPrelude, MagicHash #-}
{-# LANGUAGE DeriveDataTypeable, StandaloneDeriving #-}
module Control.Exception.Base (
SomeException(..),
Exception(..),
IOException,
ArithException(..),
ArrayException(..),
AssertionFailed(..),
SomeAsyncException(..), AsyncException(..),
asyncExceptionToException, asyncExceptionFromException,
NonTermination(..),
NestedAtomically(..),
BlockedIndefinitelyOnMVar(..),
BlockedIndefinitelyOnSTM(..),
Deadlock(..),
NoMethodError(..),
PatternMatchFail(..),
RecConError(..),
RecSelError(..),
RecUpdError(..),
ErrorCall(..),
throwIO,
throw,
ioError,
throwTo,
catch,
catchJust,
handle,
handleJust,
try,
tryJust,
onException,
evaluate,
mapException,
mask,
mask_,
uninterruptibleMask,
uninterruptibleMask_,
MaskingState(..),
getMaskingState,
assert,
bracket,
bracket_,
bracketOnError,
finally,
recSelError, recConError, irrefutPatError, runtimeError,
nonExhaustiveGuardsError, patError, noMethodBindingError,
absentError,
nonTermination, nestedAtomically,
) where
import GHC.Base
import GHC.IO hiding (bracket,finally,onException)
import GHC.IO.Exception
import GHC.Exception
import GHC.Show
import GHC.Conc.Sync
import Data.Dynamic
import Data.Either
import Data.Maybe
catch :: Exception e
=> IO a
-> (e -> IO a)
-> IO a
catch = catchException
catchJust
:: Exception e
=> (e -> Maybe b)
-> IO a
-> (b -> IO a)
-> IO a
catchJust p a handler = catch a handler'
where handler' e = case p e of
Nothing -> throwIO e
Just b -> handler b
handle :: Exception e => (e -> IO a) -> IO a -> IO a
handle = flip catch
handleJust :: Exception e => (e -> Maybe b) -> (b -> IO a) -> IO a -> IO a
handleJust p = flip (catchJust p)
mapException :: (Exception e1, Exception e2) => (e1 -> e2) -> a -> a
mapException f v = unsafePerformIO (catch (evaluate v)
(\x -> throwIO (f x)))
try :: Exception e => IO a -> IO (Either e a)
try a = catch (a >>= \ v -> return (Right v)) (\e -> return (Left e))
tryJust :: Exception e => (e -> Maybe b) -> IO a -> IO (Either b a)
tryJust p a = do
r <- try a
case r of
Right v -> return (Right v)
Left e -> case p e of
Nothing -> throwIO e
Just b -> return (Left b)
onException :: IO a -> IO b -> IO a
onException io what = io `catch` \e -> do _ <- what
throwIO (e :: SomeException)
bracket
:: IO a
-> (a -> IO b)
-> (a -> IO c)
-> IO c
bracket before after thing =
mask $ \restore -> do
a <- before
r <- restore (thing a) `onException` after a
_ <- after a
return r
finally :: IO a
-> IO b
-> IO a
a `finally` sequel =
mask $ \restore -> do
r <- restore a `onException` sequel
_ <- sequel
return r
bracket_ :: IO a -> IO b -> IO c -> IO c
bracket_ before after thing = bracket before (const after) (const thing)
bracketOnError
:: IO a
-> (a -> IO b)
-> (a -> IO c)
-> IO c
bracketOnError before after thing =
mask $ \restore -> do
a <- before
restore (thing a) `onException` after a
data PatternMatchFail = PatternMatchFail String deriving Typeable
instance Show PatternMatchFail where
showsPrec _ (PatternMatchFail err) = showString err
instance Exception PatternMatchFail
data RecSelError = RecSelError String deriving Typeable
instance Show RecSelError where
showsPrec _ (RecSelError err) = showString err
instance Exception RecSelError
data RecConError = RecConError String deriving Typeable
instance Show RecConError where
showsPrec _ (RecConError err) = showString err
instance Exception RecConError
data RecUpdError = RecUpdError String deriving Typeable
instance Show RecUpdError where
showsPrec _ (RecUpdError err) = showString err
instance Exception RecUpdError
data NoMethodError = NoMethodError String deriving Typeable
instance Show NoMethodError where
showsPrec _ (NoMethodError err) = showString err
instance Exception NoMethodError
data NonTermination = NonTermination deriving Typeable
instance Show NonTermination where
showsPrec _ NonTermination = showString "<<loop>>"
instance Exception NonTermination
data NestedAtomically = NestedAtomically deriving Typeable
instance Show NestedAtomically where
showsPrec _ NestedAtomically = showString "Control.Concurrent.STM.atomically was nested"
instance Exception NestedAtomically
recSelError, recConError, irrefutPatError, runtimeError,
nonExhaustiveGuardsError, patError, noMethodBindingError,
absentError
:: Addr# -> a
recSelError s = throw (RecSelError ("No match in record selector "
++ unpackCStringUtf8# s))
runtimeError s = error (unpackCStringUtf8# s)
absentError s = error ("Oops! Entered absent arg " ++ unpackCStringUtf8# s)
nonExhaustiveGuardsError s = throw (PatternMatchFail (untangle s "Non-exhaustive guards in"))
irrefutPatError s = throw (PatternMatchFail (untangle s "Irrefutable pattern failed for pattern"))
recConError s = throw (RecConError (untangle s "Missing field in record construction"))
noMethodBindingError s = throw (NoMethodError (untangle s "No instance nor default method for class operation"))
patError s = throw (PatternMatchFail (untangle s "Non-exhaustive patterns in"))
nonTermination :: SomeException
nonTermination = toException NonTermination
nestedAtomically :: SomeException
nestedAtomically = toException NestedAtomically