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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:44:38+00:00 2026-06-05T09:44:38+00:00

Suppose I have two functions DoTaskA and DoTaskB —both capable of throwing TaskException —with

  • 0

Suppose I have two functions DoTaskA and DoTaskB—both capable of throwing TaskException—with their corresponding “rollback” functions UndoTaskA and UndoTaskB. What is the best pattern to use so that either both succeed or both fail?

The best I have now is

bool is_task_a_done = false,
     is_task_b_done = false;

try {
    DoTaskA();
    is_task_a_done = true;

    DoTaskB();
    is_task_b_done = true;
} catch (TaskException &e) {
    // Before rethrowing, undo any partial work.
    if (is_task_b_done) {
        UndoTaskB();
    }
    if (is_task_a_done) {
        UndoTaskA();
    }
    throw;
}

I know that is_task_b_done is unnecessary, but maybe good to show code symmetry in case we add a third or a fourth task later on.

Don’t like this code because of the auxiliary boolean variables. Perhaps there is something in the new C++11 that I’m not aware of, which can code this up more nicely?

  • 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-05T09:44:40+00:00Added an answer on June 5, 2026 at 9:44 am

    A little RAII commit/rollback scope guard might look like this:

    #include <utility>
    #include <functional>
    
    class CommitOrRollback
    {
        bool committed;
        std::function<void()> rollback;
    
    public:
        CommitOrRollback(std::function<void()> &&fail_handler)
            : committed(false),
              rollback(std::move(fail_handler))
        {
        }
    
        void commit() noexcept { committed = true; }
    
        ~CommitOrRollback()
        {
            if (!committed)
                rollback();
        }
    };
    

    So, we’re assuming we’ll always create the guard object after the transaction succeeds, and call commit only after all the transactions have succeeded.

    void complicated_task_a();
    void complicated_task_b();
    
    void rollback_a();
    void rollback_b();
    
    int main()
    {
        try {
            complicated_task_a();
            // if this ^ throws, assume there is nothing to roll back
            // ie, complicated_task_a is internally exception safe
            CommitOrRollback taskA(rollback_a);
    
            complicated_task_b();
            // if this ^ throws however, taskA will be destroyed and the
            // destructor will invoke rollback_a
            CommitOrRollback taskB(rollback_b);
    
    
            // now we're done with everything that could throw, commit all
            taskA.commit();
            taskB.commit();
    
            // when taskA and taskB go out of scope now, they won't roll back
            return 0;
        } catch(...) {
            return 1;
        }
    }
    

    PS. As Anon Mail says, it’s better to push all those taskX objects into a container if you have many of them, giving the container the same semantics (call commit on the container to have it commit each owned guard object).


    PPS. In principle, you can use std::uncaught_exception in the RAII dtor instead of explicitly committing. I prefer to explicitly commit here because I think it’s clearer, and also works correctly if you exit scope early with a return FAILURE_CODE instead of an exception.

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

Sidebar

Related Questions

Suppose I have two functions which look like this: public static void myFunction1(int a,
Suppose I have two classes that both contain a static variable, XmlTag. The second
Suppose I have two functions: Foo(params INotifyPropertyChanged[] items) { //do stuff } Foo<T>(IEnumerable<T> items)
Suppose I have a file insert.c in which two functions are defined: 1.insert_after 2.insert_before
Suppose you have a Django view that has two functions: The first function renders
Suppose I have a DB of users and I have two main functions: to
Suppose I have two C++ functions for debug output: void Trace( const wchar_t* format,
Suppose I have two lists, and corresponding elements of the lists are the same
I have two onSelect functions that are both working individually, but is it possible
For example suppose I have the following two functions function a(param1) { console.log(param1); }

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.