Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt and error handling strategy

Tags:

c++

exception

qt

Actually, I do understand major pros and cons of using exceptions. And I use them in my projects by default as error-handling strategy. But now I'm starting a Windows CE project with Qt library, and I see that Qt creators refused to use exceptions in the class hierarchy.

So, if I use exceptions I will need to carefully translate them to and from error codes (or some objects, or just swallow) on my/Qt code bounds. Otherwise, I can refuse to use exceptions in my code and switch to some other strategy.

What would be the best error handling strategy in my case - to use exceptions or to use error-codes, or etc...? Do you have experience with Qt development and what error handling strategy did you use?

like image 533
Alex Che Avatar asked Oct 16 '09 14:10

Alex Che


People also ask

How do you handle exceptions in Qt?

Currently, the only supported use case for recovering from exceptions thrown within Qt (for example due to out of memory) is to exit the event loop and do some cleanup before exiting the application. Typical use case: QApplication app(argc, argv); ... int ret; try { ret = app.

What is exception handling strategy?

An exception handling strategy consists of all actions and conventions necessary to properly handle exceptions in your application. The following diagram shows an overview of the various parts of an exception handling strategy, from the error is detected, all the way up to where the error is handled.

What is the difference between error handling and exception handling?

Both exceptions and errors are the subclasses of a throwable class. The error implies a problem that mostly arises due to the shortage of system resources. On the other hand, the exceptions occur during runtime and compile time.


1 Answers

Override QApplication::notify() and handle exceptions there (not 100% on the return value). You can "throw" exceptions from signal handlers but they don't get propagated to Qt in this way.

bool
notify(QObject * rec, QEvent * ev)
{
  try
  {
    return QApplication::notify(rec,ev);
  }
  catch(my::Exception & e)
  {
    QMessageBox::warning(0,
                         tr("An error occurred"),
                         e.message());
  }
  catch(...)
  {
    QMessageBox::warning(0,
                         tr("An unexpected error occurred"),
                         tr("This is likely a bug."));
  }
  return false;
like image 109
Sohail Avatar answered Sep 24 '22 08:09

Sohail