Search code examples
c++qtqwidgetqtguiqmainwindow

Stop code execution on error (C++)


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.

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.

If there is another way to implement this please let me know. Please also let me know if you need more details.

Thanks in advance,

Here is some code which shows what my problem is

#include <iostream>
#include <string>
#include <cstdlib>
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 << "Enter command: "; 
    cin >> command; 

    if(command.compare("create_sphere") == 0)  {
      float radius; 
      cout << "Enter radius: "; 
      cin >> radius;
      create_sphere(radius); 
    }
    else if(command.compare("create_rect") == 0)  {
      float l, w; 
      cout << "Enter length and width: "; 
      cin >> l >> w; 
      create_rect(l, w); 
    }
    else if(command.compare("quit") == 0)
      break; 
  } 
}

void create_sphere(float  radius)
{
  if(radius < 0)
    error_func(string("Radius must be positive")); 

  cout << "Created sphere" << endl; 
}

void create_rect(float length, float width)
{
  if(length < 0)
    error_func(string("Length must be positive")); 

  if(width < 0)
    error_func(string("Width must be positive")); 

  cout << "Created rectangle" << endl;
} 

void error_func(string error)
{
  // do some cleanup
  cout << "ERROR: " << error << endl; 
  exit(1); 
}

Assume that create_sphere, create_rect and error_func are provided by the library. I can modify error_func as required but not the other functions (since there are many such functions).

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.


Solution

  • 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.

    Basically, you are looking for an event loop. The typical minimal Qt program is as follows:

    #include <QApplication>
    #include <QMainWindow>
    
    int main(int argc, char **argv)
    {
        QApplication(argc, argv);
        QMainWindow w;
        w.show();
        return application.exec(); // What you want instead of exit
    }
    

    Now, you could replace QMainWindow with your own class, and declare a slot in that which gets called when you are trying to handle a command from the user interface.

    #include <QWidget>
    
    ...
    
    class MyWidget : public QWidget
    {
        Q_OBJECT
        public:
            explicit MyWidget(QWidget *parent) : QWidget(parent)
            {
                connect(sender, SIGNAL(mySignal()), SLOT(handleCommand()));
            }
        public slots:
            void handleCommand()
            {
                // Handle your command here.
                // Print the error code.
                qDebug() << error_func(string("Radius must be positive"));
                // or simply:
                qDebug() << "Radius must be positive";
            } // Leaving the scope, and getting back to the event loop
    }
    

    As for the bogus library, well, if it exits, it does. There is not much you can do about that without fixint the library. It is a very bad behavior from most of the libraries.

    The modification would be not to exit, but return an error code - which is a general practice in Qt software - and leave it with the application when to exit if they wish.

    The application would not quit in your case. Again, It is a very bad idea for a library function to exit. Even Qt does not do except 1-2 times in a few million LOC.

    I would suggest not to throw an exception. It is generally not common in Qt software, and you could make your software consistent by just using error codes like the rest of Qt does for you.