Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4623100
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:56:16+00:00 2026-05-22T02:56:16+00:00

I need help to understand the usage of the three Haskell functions try (

  • 0

I need help to understand the usage of the three Haskell functions

  • try (Control.Exception.try :: Exception e => IO a -> IO (Either e a))
  • catch (Control.Exception.catch :: Exception e => IO a -> (e -> IO a) -> IO a)
  • handle (Control.Exception.handle :: Exception e => (e -> IO a) -> IO a -> IO a)

I need to know several things:

  1. When do I use which function?
  2. How do I use this function with some simple example?
  3. Where is the difference between catch and handle? They have nearly the same signature only with a different order.

I will try to write down my trials and hope you can help me:

try

I have an example like:

x = 5 `div` 0
test = try (print x) :: IO (Either SomeException ())

I have two questions:

  1. How can I set a custom error output?

  2. What can i do to set all errors to SomeException so I dont must write the :: IO (Either SomeException())

catch/try

Can you show me a short example with a custom error output?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:56:17+00:00Added an answer on May 22, 2026 at 2:56 am

    When do I use which function?

    Here’s the recommendation from the Control.Exception documentation:

    • If you want to do some cleanup in the event that an exception is raised, use finally, bracket or onException.
    • To recover after an exception and do something else, the best choice is to use one of the try family.
    • … unless you are recovering from an asynchronous exception, in which case use catch or catchJust.

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

    try takes an IO action to run, and returns an Either. If the computation succeeded, the result is given wrapped in a Right constructor. (Think right as opposed to wrong). If the action threw an exception of the specified type, it is returned in a Left constructor. If the exception was not of the appropriate type, it continues to propagate up the stack. Specifying SomeException as the type will catch all exceptions, which may or may not be a good idea.

    Note that if you want to catch an exception from a pure computation, you will have to use evaluate to force evaluation within the try.

    main = do
        result <- try (evaluate (5 `div` 0)) :: IO (Either SomeException Int)
        case result of
            Left ex  -> putStrLn $ "Caught exception: " ++ show ex
            Right val -> putStrLn $ "The answer was: " ++ show val
    

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

    catch is similar to try. It first tries to run the specified IO action, but if an exception is thrown the handler is given the exception to get an alternative answer.

    main = catch (print $ 5 `div` 0) handler
      where
        handler :: SomeException -> IO ()
        handler ex = putStrLn $ "Caught exception: " ++ show ex
    

    However, there is one important difference. When using catch your handler cannot be interrupted by an asynchroneous exception (i.e. thrown from another thread via throwTo). Attempts to raise an asynchroneous exception will block until your handler has finished running.

    Note that there is a different catch in the Prelude, so you might want to do import Prelude hiding (catch).

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

    handle is simply catch with the arguments in the reversed order. Which one to use depends on what makes your code more readable, or which one fits better if you want to use partial application. They are otherwise identical.

    tryJust, catchJust and handleJust

    Note that try, catch and handle will catch all exceptions of the specified/inferred type. tryJust and friends allow you to specify a selector function which filters out which exceptions you specifically want to handle. For example, all arithmetic errors are of type ArithException. If you only want to catch DivideByZero, you can do:

    main = do
        result <- tryJust selectDivByZero (evaluate $ 5 `div` 0)
        case result of
            Left what -> putStrLn $ "Division by " ++ what
            Right val -> putStrLn $ "The answer was: " ++ show val
      where
        selectDivByZero :: ArithException -> Maybe String
        selectDivByZero DivideByZero = Just "zero"
        selectDivByZero _ = Nothing
    

    A note on purity

    Note that this type of exception handling can only happen in impure code (i.e. the IO monad). If you need to handle errors in pure code, you should look into returning values using Maybe or Either instead (or some other algebraic datatype). This is often preferable as it’s more explicit so you always know what can happen where. Monads like Control.Monad.Error makes this type of error handling easier to work with.


    See also:

    • Control.Exception
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand aggregate functions and I need help. So for instance
I read the SDK document, but can not understand some details, need some help.
I need help to understand the following command. tar -cvf /dev/nst0 /home/user1 >> file1.log
I need your help to understand this better. This is my case. I have
I need help explaing the following example below (c) CommonsWare. I know it makes
I need help interpreting the meaning of a regular expression. I'm trying to understand
I have a very basic question and need help. I am trying to understand
I need help to understand how a function is working;: it is a recursive
I need an help to understand this problem I am facing...and apologies if it
Hi I need help to understand the decode part of a query that goes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.