catch

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.
Same as upstream catch, but will not catch asynchronous exceptions
Catch an exception.
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.
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 in the Haxl monad
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.
Catch an exception. Some care must be taken. Remember that even though it is constrained by the Exception typeclass, ε does not stand for "any" exception type; is has a concrete type when it gets to being used in your code. Things are fairly straight-forward if you know exactly the exception you are looking for:
catch
action
(\(e :: FirstWorldProblem) -> do
...
)
but more awkward when you don't. If you just need to catch all exceptions, the pattern for that is as follows:
catch
action
(\(e :: SomeException) -> do
...
)
The SomeException type is the root type of all exceptions; or rather, all types that have an instance of Exception can be converted into this root type. Thus you can catch all synchronous exceptions but you can't tell which type of exception it was originally; you rely on the Show instance (which is the default that displayException falls back to) to display a message which will hopefully be of enough utility to figure out what the problem is. In fairness it usually is. (This all seems a bit of a deficiency in the underlying exception machinery but it's what we have) This catch function will not catch asynchonous exceptions. If you need to do that, see the more comprehensive exception handling facilities offered by safe-exceptions, which in turn builds on exceptions and base). Note that Program implements MonadCatch so you can use the full power available there if required.
Generalized version of catch.