How to implement final conditions properly?
<p>This is what I'm trying to do (this is a simplification of a real project):</p>

<pre><code>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..";
  }
}
</code></pre>

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

<p>The problem appears when <code>isolated()</code> is called back from <code>process()</code> with another parameter. I want to catch this situation, but don't know how to do it, since <code>finally</code> is absent in C++. I feel that I should use <a href="http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization" rel="nofollow">RAII</a> technique, but can't figure out how to do it in this case properly.</p>

<p>This is how I can make it with code duplication:</p>

<pre><code>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..";
  }
}
</code></pre>
