Qt and error handling strategy

2020-02-25 08:37发布

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?

标签: c++ qt exception
2条回答
该账号已被封号
2楼-- · 2020-02-25 08:39

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

查看更多
别忘想泡老子
3楼-- · 2020-02-25 08:42

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;
查看更多
登录 后发表回答