extra-0.5.1: Extra functions I use.

Safe HaskellSafe-Inferred

Control.Exception.Extra

Contents

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.

Synopsis

Documentation

retry :: Int -> IO a -> IO aSource

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"

showException :: Show e => e -> IO StringSource

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 StringSource

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

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

Exception catching/ignoring

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

Ignore any exceptions thrown by the action.

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

catch_ :: IO a -> (SomeException -> IO a) -> IO aSource

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

handle_ :: (SomeException -> IO a) -> IO a -> IO aSource

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 aSource

Like catch_ but for catchJust

handleJust_ :: (SomeException -> Maybe b) -> (b -> IO a) -> IO a -> IO aSource

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 aSource

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 $ return "")

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

Like catchBool but for handle.

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

Like catchBool but for try.