Sunday, March 20, 2011

Qt and error handling strategy

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?

From stackoverflow
  • Throwing exceptions out of an event handler is not supported in Qt. Avoid that, and there should not be any problem with exceptions.

    Alex Che : Does cheez say it is actually supported?
    cheez : No, not supported. Just that you can use them at your own risk.
  • 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;
    
    Alex Che : So, this method will allow us to throw exceptions from event handlers. But what about slots (with direct connection and queued connection)?
    cheez : You'll have to test...
    Ringding : That's what we've been doing since Qt 4.0, works nicely!

0 comments:

Post a Comment