catch -package:core-program

This is the simplest of the exception-catching functions. It takes a single argument, runs it, and if an exception is raised the "handler" is executed, with the value of the exception passed as an argument. Otherwise, the result is returned as normal. For example:
catch (readFile f)
(\e -> do let err = show (e :: IOException)
hPutStr stderr ("Warning: Couldn't open " ++ f ++ ": " ++ err)
return "")
Note that we have to give a type signature to e, or the program will not typecheck as the type is ambiguous. While it is possible to catch exceptions of any type, see the section "Catching all exceptions" (in Control.Exception) for an explanation of the problems with doing so. For catching exceptions in pure (non-IO) expressions, see the function evaluate. Note that due to Haskell's unspecified evaluation order, an expression may throw one of several possible exceptions: consider the expression (error "urk") + (1 `div` 0). Does the expression throw ErrorCall "urk", or DivideByZero? The answer is "it might throw either"; the choice is non-deterministic. If you are catching any type of exception then you might catch either. If you are calling catch with type IO Int -> (ArithException -> IO Int) -> IO Int then the handler may get run with DivideByZero as an argument, or an ErrorCall "urk" exception may be propagated further up. If you call it again, you might get the opposite behaviour. This is ok, because catch is an IO computation.
Provide a handler for exceptions thrown during execution of the first action. Note that type of the type of the argument to the handler will constrain which exceptions are caught. See Control.Exception's catch.
Catch a synchronous (but not asynchronous) exception and recover from it. This is parameterized on the exception type. To catch all synchronous exceptions, use catchAny.
Same as upstream catch, but will not catch asynchronous exceptions
Generalized version of catch. Note, when the given computation throws an exception any monadic side effects in m will be discarded.
Recover from an error that might have been thrown in the higher-order action given by the first argument by passing the error to the handler given by the second argument.
Provide a handler for exceptions thrown during execution of the first action. Note that type of the type of the argument to the handler will constrain which exceptions are caught. See Control.Exception's catch.
Deprecated: Use Control.Monad.Catch.catch instead
Lifted catch.
Catch an exception.
Catch an exception. This works just like catch, but it also will attempt to catch AnnotatedException e. The annotations will be preserved in the handler, so rethrowing exceptions will retain the context. Let's consider a few examples, that share this import and exception type.
import qualified Control.Exception.Safe as Safe
import Control.Exception.Annotated

data TestException deriving (Show, Exception)
We can throw an exception and catch it as usual.
throw TestException `catch` \TestException ->
putStrLn "ok!"
We can throw an exception and catch it with annotations.
throw TestException `catch` \(AnnotatedException anns TestException) ->
putStrLn "ok!"
We can throw an exception and catch it as a AnnotatedException SomeException.
throw TestException `catch` \(AnnotatedException anns (e :: SomeException) ->
putStrLn "ok!"
Like catch, but uses MonadUnliftIO instead of MonadCatch.
Catch an exception. This is only required to be able to catch exceptions raised by throw, unlike the more general Control.Exception.catch function. If you need to be able to catch all errors, you will have to use IO.
Recover from an error that might have been thrown in the higher-order action given by the first argument by passing the error to the handler given by the second argument.
Generalized version of catch.
Provide a handler for exceptions thrown in the given action in the given exception capability.
Same as upstream catch, but will not catch asynchronous exceptions
Old-style catch.