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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:52:51+00:00 2026-05-15T19:52:51+00:00

This is what I’m trying to do (this is a simplification of a real

  • 0

This is what I’m trying to do (this is a simplification of a real project):

int param;
int result;
void isolated(int p) {
  param = p;
  try {
    // make calculations with "param" and place the
    // result into "result"
    process();
  } catch (...) {
    throw "problems..";
  }
}

I can’t change the way process() works, since this function is not created in the project and is a third-party function. It works with global variables param and result and we can’t change this.

The problem appears when isolated() is called back from process() with another parameter. I want to catch this situation, but don’t know how to do it, since finally is absent in C++. I feel that I should use RAII technique, but can’t figure out how to do it in this case properly.

This is how I can make it with code duplication:

int param;
int result;
void isolated(int p) {
  static bool running;
  if (running) {
    throw "you can't call isolated() from itself!";
  }
  running = true;
  param = p;
  try {
    // make calculations with "param" and place the
    // result into "result"
    process();
    running = false;
  } catch (...) {
    running = false; // duplication!
    throw "problems..";
  }
}
  • 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-15T19:52:52+00:00Added an answer on May 15, 2026 at 7:52 pm

    I still don’t quite sure how finally is related here, but you could try Boost.ScopeExit if you want to avoid creating a scope guard structure yourself.

    Example:

    #include <boost/scope_exit.hpp>
    #include <cstdio>
    
    int isolated(int p) {
        static bool running = false;
        if (running) {
            printf("Throwing %d\n", p);
            throw p;
        }
        printf("Starting %d\n", p);
        running = true;
        BOOST_SCOPE_EXIT( (p)(&running) ) {   // <--
            printf("Stopping %d\n", p);       // <--
            running = false;                  // <--
        } BOOST_SCOPE_EXIT_END                // <--
    
        // ...
        if (p)
            isolated(p*10);
        // ...
        printf("Returing %d\n", p);
    
        return 4;
    }
    
    int main() {
        printf(">> first\n");
        isolated(0);
        printf(">> second\n");
        try {
            isolated(1);
            printf(">> third (should not be printed.)\n");  
        } catch(int p) {
            printf("Caught %d\n", p);
        }
        isolated(0);
        printf(">> fourth\n");
    
        return 0;
    }
    

    Result:

    >> first
    Starting 0
    Returing 0
    Stopping 0
    >> second
    Starting 1
    Throwing 10
    Stopping 1
    Caught 10
    Starting 0
    Returing 0
    Stopping 0
    >> fourth
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This is my code in C : #include<stdio.h> int main() { printf(hello,world); } When
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
this gives me an error, Bad Reciever type 'NSIntegar (aka 'int'). NSString *tempTag =
This works fine, but I want to make it prettier - and accommodate all
This may be a very stupid question, but as I'll try it anyway. I'm
This is really just for my own use: I would like to be able
This somehow simple task is not so simple. I can get the number of
This has been a rather problematic issue on numerous occasions. We have alot of

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.