问题描述: 在写程序的过程中,有异常处理机制的需求。因为程序是CS模式,涉及到网络连接和数据库查询。当网络掉线或者数据库未打开,就抛出异常。然后再需要执行的地方去catch异常。可是出现了问题:没有按预期的那样捕获到异常,而且程序退出了,本来是打算异常发生后,捕获处理掉的。。运行过程中output中输出以下内容:
- Qt has caught an exception thrown from an event handler. Throwing
- exceptions from an event handler is not supported in Qt. You must
- reimplement QApplication::notify() and catch all exceptions there.
- QWaitCondition: Destroyed while threads are still waiting
原来写了个小测试程序,能够达到我的预期目标。。。代码和大家分享下:
- #include "widget.h"
- #include <QMessageBox>
- #include <QLabel>
- Widget::Widget(QWidget *parent)
- : QWidget(parent)
- {
- try
- {
- int a = 10,b = 11;
- int c = biggerthan(a,b);
- QLabel* label = new QLabel(this);
- QString s;
- s.setNum(c);
- label->setText(s);
- label->show();
- }
- catch(QString exception)
- {
- QString s = exception;
- QMessageBox::about(0,"error",s);
- }
- }
- Widget::~Widget()
- {
- }
- int Widget::biggerthan(int a,int b)
- {
- if(a<b)
- {
- throw tr("you were so foolish");
- }
- else
- {
- return a-b;
- }
- }
如何解决: 提示说用 QApplication::notify() 重新实现。。。不知道怎么弄,请大家赐教。谢谢!
[ 此帖被午小夜在2009-11-19 14:35重新编辑 ]