问题描述
其实,我明白使用例外的主要优点和缺点。我在我的项目中默认使用它们作为错误处理策略。
但现在我开始一个Windows CE项目与Qt库,我看到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.
如果我使用异常,我需要仔细翻译他们/从错误代码(或一些对象,或只是吞下)在我/ Qt代码界限。否则,我可以拒绝在我的代码中使用异常并切换到其他策略。
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.
在我的情况下,最好的错误处理策略 - 使用异常或使用错误代码,等等...?
你有Qt开发的经验和你使用什么错误处理策略?
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?
推荐答案
覆盖QApplication :: notify()并处理异常(不是100%的返回值)。您可以从信号处理程序抛出异常,但不会以这种方式传播到Qt。
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;
这篇关于Qt和错误处理策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!