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 8700119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:06:49+00:00 2026-06-13T02:06:49+00:00

I have a class that distributes work given by the user to several threads.

  • 0

I have a class that distributes work given by the user to several threads. Something like (simplified):

class MT {
public:

    MT();

    void work1(/*args*/);
    void work2(/*args*/);
    void work3(/*args*/);

    //waits until all threads complete, returns whether the work has been completed
    //successfully, throws any exceptions that have been raised in the threads.
    bool is_ok(); 

    ~MT();

private:
    // thread pool
}

The user uses the class as follows:

void foo()
{
    MT mt;
    mt.work1(/*userdata*/);
    mt.work2(/*userdata*/);
    mt.work1(/*userdata*/);
    status = mt.is_ok();
    if (status) {
        mt.work3(/*userdata*/);
        //...
    }
    //...
}

The class is never part of some object, always stored on the stack.

The Problem

I want to somehow emit any exceptions that have been raised while doing work in other threads. Unfortunately, I know whether a thread has successfully completed only after joining it. Thus, I must choose between the following two alternatives:

  • Join the threads in the destructor of MT and throw exceptions that arose while doing work, if any. If more than one exception has been thrown, pick the one that came from the earliest task. If the destructor is called doing stack unwinding (we can check this using std::uncaught_exception, swallow any exceptions to prevent terminate().

  • Instruct the user to always call is_ok before the destructor.

I think the first option is cleaner, since the user is not required to call anything. However, throwing from destructor is usually very strongly discouraged. The presented arguments are:

  • Throwing from destructors is dangerous, because this might happed during stack unwinding and lead to terminate().
  • Even if the above is addressed, throwing or not throwing depending on whether stack is being unwound is a substantial change of behavior and should be discouraged.
  • Exceptions signify that a postcondition have not been met. The postcondition of a destructor is resource cleanup, this must be possible in any circumstances.

Somehow I am inclined to think that the above arguments don’t apply here:

  • I handle the stack unwinding problem properly
  • Due to the usage pattern, any exceptions that exit from foo means that work has been failed. Throwing or not throwing in the destructor is not a substantial change of behavior.
  • The postconditions of the destructor are not only resource cleanup, but also that the work has been completed successfully. Thus an exception should be appropriate.

Questions

  • Do you think throwing from destructor is appropriate in this case?
  • 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-13T02:06:51+00:00Added an answer on June 13, 2026 at 2:06 am

    No, don’t throw from the destructor. Call is_ok, catch and ignore. Same as close() for streams.

    The user can call is_ok if they want to be sure that the work has been completed, and it will throw an exception on failure.

    In practice this is rarely inconvenient. It will be if the user writes a function with multiple return points, but (unfortunately) that’s a problem they just have to deal with because C++ doesn’t provide a means for you to do it for them. If you think this is anti-social, well, look at what happens if you destroy a std::thread in C++11 without first either joining it or detaching it. You’re beating that 🙂

    So, user calls is_ok on all non-error exit paths. User doesn’t bother calling it when there’s already an exception, because they can’t deal with another one anyway. Again, this is the same as close() for streams: if you want to see errors from writing your buffered stream then you just have to close or flush explicitly.

    Even if the above is addressed, throwing or not throwing depending on
    whether stack is being unwound is a substantial change of behavior and
    should be discouraged.

    It’s also not possible to do correctly, at least in C++03. I don’t know whether C++11 changes anything in this respect, but std::uncaught_exception does not tell you what you need to know, as Herb Sutter explains.

    From your link to cppreference.com:

    Any exception thrown while std::uncaught_exception() == true calls std::terminate.

    I’m pretty sure this is false. terminate is called if an exception escapes a destructor that is called as part of stack unwinding, but it isn’t called just because a destructor throws and catches an exception during unwinding.

    If the caller thinks they can do something useful with an exception thrown in a destructor, then they can write a helper class that calls is_ok in its destructor, and put that over your object. If you think there’s something useful that your class can do with the same exception then you can do something other than merely ignore it in your destructor, but you still shouldn’t allow it to leave the destructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class that looks like this: class A { public: class variables_map vm
I have a Class that has a private variable with a public setter/getter function:
I have a class that has several member classes as attributes. The constructor of
I have a class that I want to contain multiple objects of something I
I have a helper class that distributes a shared instance of UIManagedDocument. The idea
I have a class that manages user preferences for a large software project. Any
I have a class library that contains my object model. I'd like each object
I have class that extend FragmentActivity in it I add fragment to layout as
I have class that represents users. Users are divided into two groups with different
I have class grades that I need to check if a specific grade is

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.