开发者

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?


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;


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜