title: Stop code execution on error (C++)
body: <p>I have a library written in C++. The library has a function which accepts commands as a string and executes them. If an error is encountered (either in the command or while running the command) an "error function" is called which does some cleanup and finally calls exit(1). I am now trying to implement a graphical user interface (using Qt) to the library. The problem is that when an error is encountered, exit is called and my application crashes. I have access to the library source code but I would like to keep modifying the source code to minimum. </p>
  
  <p>I am thinking of rewriting the error function such that it just stops executing code and stays in an idle state until another command is passed to the library from the user-interface. The problem is I am not sure how to go about doing it. I am basically looking for a function call equivalent to exit system call (so that the error function never returns to the code which generated the error) except that I do not want the application to exit but instead just go to an idle state and wait for calls from the user interface. </p>
  
  <p>If there is another way to implement this please let me know. Please also let me know if you need more details. </p>
  
  <p>Thanks in advance, </p>
  
  <p>Here is some code which shows what my problem is</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;cstdlib&gt;
  using namespace std;
  
  void error_func(string error); 
  void create_sphere(float radius); 
  void create_rect(float length, float width); 
  
  int main()
  {
    string command; 
    while(1)  {
      cout &lt;&lt; "Enter command: "; 
      cin &gt;&gt; command; 
  
      if(command.compare("create_sphere") == 0)  {
        float radius; 
        cout &lt;&lt; "Enter radius: "; 
        cin &gt;&gt; radius;
        create_sphere(radius); 
      }
      else if(command.compare("create_rect") == 0)  {
        float l, w; 
        cout &lt;&lt; "Enter length and width: "; 
        cin &gt;&gt; l &gt;&gt; w; 
        create_rect(l, w); 
      }
      else if(command.compare("quit") == 0)
        break; 
    } 
  }
  
  void create_sphere(float  radius)
  {
    if(radius &lt; 0)
      error_func(string("Radius must be positive")); 
  
    cout &lt;&lt; "Created sphere" &lt;&lt; endl; 
  }
  
  void create_rect(float length, float width)
  {
    if(length &lt; 0)
      error_func(string("Length must be positive")); 
  
    if(width &lt; 0)
      error_func(string("Width must be positive")); 
  
    cout &lt;&lt; "Created rectangle" &lt;&lt; endl;
  } 
  
  void error_func(string error)
  {
    // do some cleanup
    cout &lt;&lt; "ERROR: " &lt;&lt; error &lt;&lt; endl; 
    exit(1); 
  }
  </code></pre>
  
  <p>Assume that <code>create_sphere</code>, <code>create_rect</code> and <code>error_func</code> are provided by the library. I can modify <code>error_func</code> as required but not the other functions (since there are many such functions). </p>
  
  <p>Now when an error is encountered, I would like to go back to the while loop in main so that I can keep accepting other commands. </p>