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

  • SEARCH
  • Home
  • 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 9245047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:10:40+00:00 2026-06-18T09:10:40+00:00

I’m currently trying to wrap my mind around the correct way to use exceptions

  • 0

I’m currently trying to wrap my mind around the correct way to use exceptions in Haskell. How exceptions work is straight-forward enough; I’m trying to get a clear picture of the correct way to interpret them.

The basic position is that, in a well-designed application, exceptions shouldn’t escape to the top-level. Any exception that does is clearly one which the designer did not anticipate – i.e., a program bug (e.g., divide by zero), rather than an unusual run-time occurrence (e.g., file not found).

To that end, I wrote a simple top-level exception handler that catches all exceptions and prints a message to stderr saying “this is a bug” (before rethrowing the exception to terminate the program).

However, suppose the user presses Ctrl+C. This causes an exception to be thrown. Clearly this is not any kind of program bug. However, failing to anticipate and react to a user abort such as this could be considered a bug. So perhaps the program should catch this and handle it appropriately, doing any necessary cleanup before exiting.

The thing is, though… The code that handles this is going to catch the exception, release any resources or whatever, and then rethrow the exception! So if the exception makes it to the top-level, that doesn’t necessarily mean it was unhandled. It just means we wanted to exit quickly.

So, my question: Should exceptions be used for flow-control in this manner? Should every function that explicitly catches UserInterrupt use explicit flow-control constructs to exit manually rather than rethrow the exception? (But then how does the caller know to also exit?) Is it OK for UserInterrupt to reach the top-level? But in that case, is it OK for ThreadKilled too, by the same argument?

In short, should the interrupt handler make a special case for UserInterrupt (and possibly ThreadKilled)? What about a HeapOverflow or StackOverflow? Is that a bug? Or is that “circumstance beyond the program’s control”?

  • 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-06-18T09:10:41+00:00Added an answer on June 18, 2026 at 9:10 am

    Cleaning up in the presence of exceptions

    However, failing to anticipate and react to a user abort such as this could be considered a bug. So perhaps the program should catch this and handle it appropriately, doing any necessary cleanup before exiting.

    In some sense you are right — the programmer should anticipate exceptions. But not by catching them. Instead, you should use exception-safe functions, such as bracket.
    For example:

    import Control.Exception
    
    data Resource
    
    acquireResource :: IO Resource
    releaseResource :: Resource -> IO ()
    
    workWithResource = bracket acquireResource releaseResource $ \resource -> ...
    

    This way the resources will be cleaned up regardless of whether the program will be aborted by Ctrl+C.

    Should exceptions reach top level?

    Now, I’d like to address another statement of yours:

    The basic position is that, in a well-designed application, exceptions shouldn’t escape to the top-level.

    I would argue that, in a well-designed application, exceptions are a perfectly fine way to abort. If there are any problems with this, then you’re doing something wrong (e.g. want to execute a cleanup action at the end of main — but that should be done in bracket!).

    Here’s what I often do in my programs:

    1. Define a data type that represents any possible error — anything that might go wrong. Some of them often wrap other exceptions.

      data ProgramError
        = InputFileNotFound FilePath IOException
        | ParseError FilePath String
        | ...
      
    2. Define how to print errors in a user-friendly way:

      instance Show ProgramError where
        show (InputFileNotFound path e) = printf "File '%s' could not be read: %s" path (show e)
        ...
      
    3. Declare the type as an exception:

      instance Exception ProgramError
      
    4. Throw these exceptions in the program whenever I feel like it.

    Should I catch exceptions?

    Exceptions that you anticipate must be caught and wrapped (e.g. in InputFileNotFound) to give them more context. What about the exceptions that you don’t anticipate?

    I can see some value in printing “it’s a bug” to the users, so that they report the problem back to you. If you do this, you should anticipate UserInterrupt — it’s not a bug, as you say. How you should treat ThreadKilled depends on your application — literally, whether you anticipate it!

    This, however, is orthogonal to the “good design” and depends more on what kind of users you’re targeting, what you expect of them and what they expect of your program.
    The response may range from just printing the exception to a dialog that says “we’re very sorry, would you like to submit a report to the developers?”.

    • 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 how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am confused How to use looping for Json response Array in another Array.
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.